[英]How do I read .txt files with fstream in c++
我必须用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
<iomanip>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.