C++中如何判断文件是否存在

1 致谢

感谢网友roger_77提供的资料

链接如下:

https://blog.csdn.net/roger_77/article/details/1538447/

2 问题描述

今天在继续学习目标检测的代码 遇到一个小问题

需要判断文件是否存在 于是就在想在C++中如何实现这个小功能

然后就找到了上文中说到的资料

3解决方案

测试代码如下:

// Test_VS2013.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include
#include 
#include 

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	fstream _file;

	string path = "D:\\Test\\2.jpg";

	_file.open(path, ios::in);
	if (!_file)
	{
		cout << path << " 没有被创建" << endl;
	}
	else
	{
		cout << path << " 已经存在" << endl;
	}

	// 记得要把文件close,当时没有注意,不好意思,,,

	system("pause");

	return 0;
}

测试结果如下图

C++中如何判断文件是否存在_第1张图片

测试成功!

你可能感兴趣的:(C++中如何判断文件是否存在)