[英]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.