ifstream无法打开我的文件
ifstream is failing to open my file
我正在创建一个使用简单文本文件中信息的程序。然而,它每次都会被我的.fail()函数捕获。该文件名为Test.txt,与程序的.exe文件位于同一文件夹中。我不知道为什么它一直在点击.fail()
以下是我创建ifstream、打开文件和.fail()循环的代码。
ifstream input;
input.open(fileNameIn.c_str());
if(input.fail()){
cout << "File named " << fileNameIn << " did not open successfully." << endl;
}
尝试input.open(strSourceFile.c_str(), std::ifstream::in);
,看看这是否有帮助。
这是我使用c++读取文件时使用的模式
//
// FileAccessor is just my wrapper around ifstream
// inside the constuctor:
// ifstream.open(strSourceFile.c_str(),std::ifstream::in); is called
//
FileAccessor fileAccessor(textFile);
while(fileAccessor.good()) {
fileAccessor.getline(lineOfText);
...
if(fileAccessor.eof()) break;
}
根据http://www.cplusplus.com/reference/ios/ios/good/
bool good() const;
Check whether state of stream is good
Returns true if none of the stream's error state flags (eofbit, failbit and badbit) is set.
您可以在循环中设置一个标志,看看它是否进入过循环,如果没有,则检查错误位标志。
相关文章:
- 我的文件无法正常工作,无法从C++文件中读取上下文
- 将内容从第一个文件("constituencies")移动到第二个文件("temp")并在之后重命名时,我的文件被删除
- 为什么URLDownloadToFile()没有下载我的文件
- 使用Cygwin C++时出现未定义的引用错误,为什么我的文件没有链接?
- C++ while 循环没有从我的文件中读取 -1
- 这是我的 .h 文件的一些错误,当我在其中包含我的类模板时,它显示 [错误] 未终止 #ifndef
- 为什么我的".a"文件中的所有符号值都是 0
- 不会在登录名中读取C++我的文件
- 使用 c++ 将我的文件拆分为 128 个字节
- 我的C++文件期望用'while'代替 if。最后,它预计会出现'('
- 为什么当我写入文件时,结构在我的文件中消失了
- 视觉C++为什么我的文件在重新启动程序时被截断?
- 我的文件可以编译,但当我尝试运行它时"no such file or directory"
- 在Ostream上使用std :: endl使我的文件二进制
- 无法使用名称空间,也不能在我的.h文件中包含标准的C 库
- 为什么我的文件/目录操作在Windows上工作正常,但在Linux上不能
- 我的C文件是从Pro*C生成的吗
- C++11 fgetc 在"r+b"模式下使用时向我的文件输出 0
- Qt Android:如果我写信给"./myfile.txt",我的文件在哪里?
- 我的C++文件中是否有可能的内存泄漏