c/c++判断文件是否存在

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <io.h>
using namespace std;

int main()
{
	fstream file;
	char filename[100];
	FILE *fp;
	strcpy(filename, "test.txt");
	file.open(filename, ios::in);
	if (!file)
	{
		cout << filename << " not exist" << endl;
	}
	else
	{
		cout << filename << " exist" << endl;
	}
	file.close();

	fp = fopen(filename, "r");
	if (fp == NULL)
	{
		cout << filename << " not exist" << endl;
	}
	else
	{
		cout << filename << " exist" << endl;
	}
	fclose(fp);
	if (_access(filename, 0) != -1)
	{
		cout << filename << " exist" << endl;
	}
	else
	{
		cout << filename << " not exist" << endl;
	}
	return 0;
}
某大牛的总结:http://blog.csdn.net/roger_77/article/details/1538447
posted @ 2013-09-20 11:20  N3verL4nd  阅读(343)  评论(0编辑  收藏  举报