2011-08-01 84 views
0

我有一個簡單的文件讀取問題。我有四個四行長的T/F,一開始就有一個計數。我用myfile >> count和myfile >>值讀取數值,計數完成後離開行並轉到下一行,但出於某種原因,我遇到了第三行的問題。不知道如何在這裏獲取數據文件...感謝您的期待!C++從文件環讀取亂碼

int main() { 
    ifstream myfile; 
    int count; 
    string value; 

    myfile.open("branches.txt"); 

    while(!myfile.eof()) { 
     myfile >> count; 
     cout << count << endl; 

     while(count > 0) { 
      myfile >> value; 
      count--; 
      //cout << value; 
     } 

     myfile >> count; 
    } 

    system("pause"); 
    return 0; 
} 
+0

你能否用文件的確切內容更新這篇文章? –

回答

2

你似乎是試圖讀取count兩次每行:在while循環的開始和結束。

4

不要使用feof()它只是告訴你什麼是上一次閱讀的結果。讀取文件的正確方法是

while(read(file, buffer)) 
{  
    //do something 
} 
+1

在這種情況下'while(myfile >> count)'。 –

+2

其實在閱讀(..)> 0) –