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

版权声明:墨眉无锋,似攻非攻。 https://blog.csdn.net/songyuc/article/details/79750292

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<iostream>
#include <fstream>
#include <string>

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;
	}

	system("pause");

	return 0;
}

测试结果如下图


测试成功!

猜你喜欢

转载自blog.csdn.net/songyuc/article/details/79750292