C++ 无法读取文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13017847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Trouble Reading a Text File
提问by Howdy_McGee
I'm trying to read a text file but nothing is coming out. I feel like maybe It's not linking correctly in my Visual Studio Resources folder but if I double click it - it opens fine in visual studio and it doesn't run into any problems if I test to see if it opens or if it is good. The program compiles fine right now but there's not output. Nothing prints to my command prompt. Any suggestions?
我正在尝试读取文本文件,但没有任何内容。我觉得它可能没有在我的 Visual Studio Resources 文件夹中正确链接,但是如果我双击它 - 它在 Visual Studio 中打开得很好,如果我测试它是否打开或者它是否良好,它不会遇到任何问题。该程序现在可以正常编译,但没有输出。我的命令提示符没有打印任何内容。有什么建议?
Code
代码
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
char str[100];
ifstream test;
test.open("test.txt");
while(test.getline(str, 100, '#'))
{
cout << str << endl;
}
test.close();
return 0;
}
Text File
文本文件
This is a test Textfile#Read more lines here#and here
回答by Rost
You try to open file by name without path, this means the file shall be in current working directory of your program.
您尝试按名称打开文件而不使用路径,这意味着该文件应位于程序的当前工作目录中。
The problem is with current directory when you run your program from VS IDE. VS by default sets current working directory for runnning program to project directory $(ProjectDir)
. But your test file resides in resources directory. So open()
function could not find it and getline()
immediately fails.
当您从 VS IDE 运行程序时,问题出在当前目录上。默认情况下,VS 将运行程序的当前工作目录设置为项目目录$(ProjectDir)
。但是您的测试文件位于资源目录中。所以open()
函数找不到它并getline()
立即失败。
Solution is simple - copy your test file to project directory. Or copy it to target directory (where your program .exe
file is created, typically $(ProjectDir)\Debug
or $(ProjectDir)\Release
) and change working directory setting in VS IDE: Project->Properties->Debugging->Working Directory
, set to $(TargetDir). In this case it will work both from IDE and command line/Windows Explorer.
解决方案很简单 - 将您的测试文件复制到项目目录。或将其复制到目标目录(.exe
创建程序文件的位置,通常为$(ProjectDir)\Debug
或$(ProjectDir)\Release
)并更改 VS IDE 中的工作目录设置:Project->Properties->Debugging->Working Directory
,设置为 $(TargetDir)。在这种情况下,它将在 IDE 和命令行/Windows 资源管理器中工作。
Another possible solution - set correct path to file in your open()
call. For testing/education purposes you could hardcode it, but actually this is not good style of software development.
另一种可能的解决方案 - 在您的open()
通话中设置正确的文件路径。出于测试/教育目的,您可以对其进行硬编码,但实际上这不是一种好的软件开发风格。
回答by peterdcasey
Not sure if this will help but I wanted to simply open a text file for output and then read it back in. Visual Studio (2012) seems to make this difficult. My solution is demonstrated below:
不确定这是否会有所帮助,但我只想打开一个文本文件进行输出,然后再读回。Visual Studio (2012) 似乎使这变得困难。我的解决方案如下所示:
#include <iostream>
#include <fstream>
using namespace std;
string getFilePath(const string& fileName) {
string path = __FILE__; //gets source code path, include file name
path = path.substr(0, 1 + path.find_last_of('\')); //removes file name
path += fileName; //adds input file to path
path = "\" + path;
return path;
}
void writeFile(const string& path) {
ofstream os{ path };
if (!os) cout << "file create error" << endl;
for (int i = 0; i < 15; ++i) {
os << i << endl;
}
os.close();
}
void readFile(const string& path) {
ifstream is{ path };
if (!is) cout << "file open error" << endl;
int val = -1;
while (is >> val) {
cout << val << endl;
}
is.close();
}
int main(int argc, char* argv[]) {
string path = getFilePath("file.txt");
cout << "Writing file..." << endl;
writeFile(path);
cout << "Reading file..." << endl;
readFile(path);
return 0;
}