2014-06-24 75 views
0

我有一個文件,我需要輸入在我的程序用C++ .. 文件結構從C++中的文件獲取輸入?

100 
150 
245 467 367 367 

使用get()只讀取第一行......再次使用get()沒有幫助.. PLS建議最好的方法。 感謝

+0

100個數據在換行後...由html問題自動格式化...請考慮 – Sumit

回答

1

參見:http://en.cppreference.com/w/cpp/io/basic_ifstream

嘗試這樣:

#include <fstream> 
#include <iostream> 
#include <string> 

using namespace std; 

int main() 
{ 
    ifstream stream("file.txt"); 
    string foo; 

    while(stream >> foo) 
      cout << foo << endl; 

    return 0; 
} 

輸出:

100 
150 
245 
467 
367 
367 

可以使用流運營商在一次提取一個數字。

更新您的問題,如果這沒有回答它作爲它很難告訴你想要做什麼的一種。

+0

是的,我一次只需要一個數字.. – Sumit

+0

這應該會給你你需要的。只需替換cout << foo << endl;與你打算怎麼處理數字。當然,如果你想以數字方式做任何事情,你必須先轉換它們。 – csnate

+0

您需要將'while'中的expressin更改爲'while(stream >> foo)'。搜索StackOverflow以「讀取文件損壞」。 –