我必须用QtCreator编写一个c ++程序来评估足球桌。每个游戏的结果都保存在一个文本文件(“ Bundesliga.txt”)中。我们必须使用fstream读取文件,但无法打开它。
这是我读取文件的测试程序。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char test[10];
ifstream table;
table.open("Bundesliga.txt");
if (table.fail())
{
cerr << "Error Opening File \n";
exit(1);
}
table.getline(test, 10);
cout << test;
return 0;
}
文本文件保存在项目文件夹中,我将其导入为RESOURCES和OTHER_FILES。没事。我很高兴能提供单一帮助!
编辑:
我正在使用Qt-Creator。
最佳答案
请注意以下几点:.txt
文件必须位于源代码文件的文件夹中。
检查您在文件夹中看到的文件名不是Bundesliaga.txt
,它被解释为Bundesliga.txt.txt
。这是一个常见的错误。
询问if(!table.is_open())
您没有使用\0
将字符串置空。这样做或使用table >> setw(10)
。 setw在<iomanip>
处声明,并用\0
将字符串为空。
关于c++ - 如何在C++中使用fstream读取.txt文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50783075/