簡體   English   中英

如何在C ++中使用fstream讀取.txt文件

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

請注意以下幾點:

  1. .txt文件必須位於源代碼文件的文件夾中。
  2. 檢查您在文件夾中看到的文件名不是Bundesliaga.txt ,它被解釋為Bundesliga.txt.txt 這是一個常見的錯誤。
  3. 詢問if(!table.is_open())
  4. 您不會用\\0使字符串無效。 這樣做或使用table >> setw(10) setw在<iomanip>處聲明,並將字符串以\\0 <iomanip>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM