簡體   English   中英

C ++讀取txt文件?

[英]C++ Reading a txt file?

嗨,我使用ubuntu(Linux),使用g ++編譯器。

我有一個非常奇怪的情況,昨天我的代碼工作正常,我沒有做任何事情,但今天,它沒有工作。 這是我的代碼:

ifstream file;
file.open("users.txt", ios::in);

if(file.is_open()){
    int counter = 0;
    string readLine;
    file.seekg(0, ios::end);
    if (file.tellg() == 0)
        file.close();
    else {
        while(!file.eof()){
            getline(file,readLine);
            cout << "whats happening?" << readLine << endl;
            // I was suppose to do process here, but i comment it for debug purposes
        }
        openFile.close();
    }

我不明白為什么,我花了2個小時調試,昨天,它可以讀取用戶數據,但今天,我打開相同的項目,但它無法讀取文件。 我100%肯定,路徑是正確的,文件有內容。 我的結果是:

Whats happening?

多數民眾贊成,沒有別的。 幫助我,我瘋了看看這東西!!!!!!!!

file.seekg(0, ios::end); 將尋求文件的結尾。 你需要在開始閱讀之前回到起點即。 is.seekg(0, ios::beg);

暫無
暫無

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

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