C++用ifstream和ofstream打开文件时,如何判断文件存不存在?

huagong_adu 2008-12-19 07:11:46
我用
ifstream file;
file.open("test.txt",ios::binary);

oftream file;
file.open("test.txt",ios::binary);
的时候,如果当前目录没有test.txt时总是创建新文件,能不能判断这个文件存不存在,有什么函数可以调用吗?
请高手帮帮忙!
...全文
4646 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
fendouzhe 2010-03-07
  • 打赏
  • 举报
回复
用vs005试了一下,没有自动创建啊?
AlwaysSLH 2008-12-21
  • 打赏
  • 举报
回复
9楼正解
ofan 2008-12-21
  • 打赏
  • 举报
回复
文件不存在会返回打开失败
去查查fstream的成员就知道了
cangwu 2008-12-19
  • 打赏
  • 举报
回复
路过了,也学习一下
ckt 2008-12-19
  • 打赏
  • 举报
回复
用个api

#include "io.h"
bool is_file_exist(const char* pcsFile)
{
return pcsFile!=NULL && access(pcsFile, 0)==0;
}
matrixdwy 2008-12-19
  • 打赏
  • 举报
回复
ifstream ( );
explicit ifstream ( const char * filename, ios_base::openmode mode = ios_base::in );

Construct object and optionally open file

Constructs an object of the ifstream class. This implies the initialization of the associated
filebuf object and the call to the constructor of its base class with the filebuf object as parameter.

Additionally, when the second constructor version is used, the stream is associated with a
physical file as if a call to the member function open with the same parameters was made.

If the constructor is not successful in opening the file, the object is still created
although no file is associated to the stream buffer and the stream's failbit is set
(which can be checked with inherited member fail).
lzr4304061988012 2008-12-19
  • 打赏
  • 举报
回复
严格来说不可以,但一般不会错了。
matrixdwy 2008-12-19
  • 打赏
  • 举报
回复
http://blog.csdn.net/ruibird/archive/2007/03/23/1539098.aspx

#include <iostream>
#include <fstream>
using namespace std;
#define FILENAME "stat.dat"
int main()
{
fstream _file;
_file.open(FILENAME,ios::in);
if(!_file)
{
cout<<FILENAME<<"没有被创建";
}
else
{
cout<<FILENAME<<"已经存在";
}
return 0;
}
huagong_adu 2008-12-19
  • 打赏
  • 举报
回复
ifstream file;
file.open(filepath2,ios::binary | ios::nocreate);
这样如果没创建的话,那么怎么知道这个文件不存在?
用if(!file)...好像不行吧
huagong_adu 2008-12-19
  • 打赏
  • 举报
回复
你的可以吗?我试了你的那个,不行哦
lzr4304061988012 2008-12-19
  • 打赏
  • 举报
回复

//忽略5L
ios::nocreate: 不建立文件,所以文件不存在时打开失败
lzr4304061988012 2008-12-19
  • 打赏
  • 举报
回复
  ios::noreplace:不覆盖文件,所以打开文件时如果文件存在失败
nullah 2008-12-19
  • 打赏
  • 举报
回复
汗.....请忽略2楼的注释
nullah 2008-12-19
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
using namespace std;

void main()
{
ofstream file( "aaa.txt" );
if( file )
{
cout << "if( file ) " << endl;
}

if(file.is_open())
{
cout << "file.is_open()" << endl;
}

file.close();//
}
nullah 2008-12-19
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
using namespace std;

void main()
{
ofstream file( "aaa.txt" );
if( file )
{
cout << "if( file ) " << endl;
}

if(file.is_open())
{
cout << "file.is_open()" << endl;
}

file.close();//放开它后,为什么输入的内容写不到文件中去.
}
nullah 2008-12-19
  • 打赏
  • 举报
回复
file.isopen()

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧