2013-04-30 54 views
-3

我想跳轉/循環到文本文件的最後一行,只讀取該行的第一個字符並將其存儲在一個名爲「id 」。有人可以解釋我將如何實現這一目標嗎?文本文件的內容如下:C++跳轉/循環到文本文件的最後一行

1 Chris Boy 5 
2 Stephanie Girl 8 
3 Zack Boy 1 
+0

檢查這篇文章有關如何讀取文件的只有最後一行:http://stackoverflow.com/questions/11876290/c-fastest-way-to-read-only-last -line-of-text-file – taocp 2013-04-30 02:51:39

回答

-1

我設法使用提供的鏈接tacp來解決這個問題。我的代碼如下:

if (inStream.is_open()) 
    { 
     inStream.seekg(-1, ios_base::end); 

     getline(inStream, lastLine); 

     id = stoi(lastLine); 

     id = id + 1; 
    } 
    else 
    { 
     cout << "Unable to open staffMembers.txt.\n"; 
     exit(1); 
    } 
+0

這將不起作用。 – Matt 2013-04-30 04:10:26

相關問題