fstream打开失败后重新打开新文件一个问题
2012-09-17 22:59
393 查看
请看这则代码,看看你能不能发现其中的错误:
在测试过程中发现,即使是全英文的绝对路径,这里也是死循环。后来在CSDN看到http://topic.csdn.net/u/20101014/09/8AF9EB40-3D8F-4931-BA41-CACD891C35D1.html
这个帖子,才知道错在哪里。
先贴上正确的代码:
其中的 fileStream.clear();就是问题的关键所在。
让我们看看 fstream::open的参考:http://www.cplusplus.com/reference/iostream/fstream/open/
原文:
Open file
Opens a file whose name is filename, associating its content
with the stream object to perform input/output operations on it. The operations allowed and some operating details depend on parameter mode.
The function effectively calls rdbuf()->open(filename,mode).
If the object already has a file associated (open), the function fails.
On failure, the failbit flag is set (which can be checked with member fail),
and depending on the value set with exceptions an
exception may be thrown.
后面红色这句是说,打开失败的话,失败flag会被置位,并根据设置的值可能会抛出异常,
再看看fstream::clear
Set error state flags
Sets a new value for the error control state.
All the bits in the control state are replaced by the new ones;
The value existing before the call has no effect.
If the function is called with goodbit as argument (which is the default value) all error flags are cleared.
The current state can be obtained with member function rdstate.
这里是给 错误状态 重新设定一个新的置,并且默认是goodbit。也就是说,一开始fstream中的errorState应该是goodbit,如果打开失败了,就设置为其他值,fail()中可以根据这个值检测打开是否成功。但是如果打开成功了,并不会设置这个值为新的值,必须手工调用clear重置。
ifstream fileStream(fileName.c_str()); while (fileStream.fail()) { string tmpFileName; cout<<"打开文件失败!请输入文件名:"<<endl; cin>>tmpFileName; fileStream.open(tmpFileName.c_str(),ios::in); }
在测试过程中发现,即使是全英文的绝对路径,这里也是死循环。后来在CSDN看到http://topic.csdn.net/u/20101014/09/8AF9EB40-3D8F-4931-BA41-CACD891C35D1.html
这个帖子,才知道错在哪里。
先贴上正确的代码:
ifstream fileStream(fileName.c_str()); while (fileStream.fail()) { string tmpFileName; cout<<"打开文件失败!请输入文件名:"<<endl; cin>>tmpFileName; ////////////////////////////////////////////////////////////////////////// fileStream.clear(); ////////////////////////////////////////////////////////////////////////// fileStream.open(tmpFileName.c_str(),ios::in); }
其中的 fileStream.clear();就是问题的关键所在。
让我们看看 fstream::open的参考:http://www.cplusplus.com/reference/iostream/fstream/open/
原文:
Open file
Opens a file whose name is filename, associating its content
with the stream object to perform input/output operations on it. The operations allowed and some operating details depend on parameter mode.
The function effectively calls rdbuf()->open(filename,mode).
If the object already has a file associated (open), the function fails.
On failure, the failbit flag is set (which can be checked with member fail),
and depending on the value set with exceptions an
exception may be thrown.
后面红色这句是说,打开失败的话,失败flag会被置位,并根据设置的值可能会抛出异常,
再看看fstream::clear
void clear ( iostate state = goodbit );
Set error state flags
Sets a new value for the error control state.
All the bits in the control state are replaced by the new ones;
The value existing before the call has no effect.
If the function is called with goodbit as argument (which is the default value) all error flags are cleared.
The current state can be obtained with member function rdstate.
这里是给 错误状态 重新设定一个新的置,并且默认是goodbit。也就是说,一开始fstream中的errorState应该是goodbit,如果打开失败了,就设置为其他值,fail()中可以根据这个值检测打开是否成功。但是如果打开成功了,并不会设置这个值为新的值,必须手工调用clear重置。
相关文章推荐
- 用fstream打开含中文路径文件时失败的问题。
- Win7、Windows Server 2008下无法在Windows Service中打开一个已经存在的Excel 2007文件问题的解决方案
- fopen打开文件失败的问题
- [转]Win7、Windows Server 2008下无法在Windows Service中打开一个已经存在的Excel 2007文件问题的解决方案
- VS 2010下 打开实例 LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏的问题
- 关于as中adb打开失败的一个小问题
- 未能加载文件或程序集或它的某一个依赖项(针对资源问题,x86文件在x64上编译失败)
- fstream对象重复使用时注意clear()的调用,否则会造成打开第二个文件失败
- 一个打开文件时可以改进的细节(window,linux都有这个问题)
- fstream对象重复使用时注意clear()的调用,否则会造成打开第二个文件失败
- fstream打开一个文件时如何清空文件的内容
- win7 chm后缀文件打开失败问题.
- 重新打包apk问题:jarsigner: 无法打开 jar 文件:
- 一个Download.jsp文件 (采用文件流读写方式。可以防止doc.excl等文件打开时乱码问题)
- 重新想象 Windows 8 Store Apps (33) - 关联启动: 使用外部程序打开一个文件或uri, 关联指定的文件类型或协议
- iOS 开发之一个挺操蛋的问题,Xcode刚打开工程文件展示区显示不出所有文件。
- win7中打开文件夹会重新打开一个窗口的问题
- windows server 2008下无法用程序打开一个xls文件问题
- 关于用excel打开一个utf-8格式的csv文件的问题
- 多个文件合并成一个大文件后,无法打开,需要从以下几个方面找出问题?