1
我有一個文本文件,我需要讀入我的代碼中的變量。例如可以說.txt
文件看起來像:readline in C++?
John
Town
12
Mike
Village
22
在有名稱的圖案,然後再解決多歲的人。我發現,(`)
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (getline (myfile,line))
{
cout << line << '\n';
}
myfile.close();
}
我可以打印出的文本文件的每一行,但我怎麼能分配文本變量? 我記得在Java中,你可以沿着
while(there is a next line){
name = something.readline();
address = something.readline();
age = something.readline();
//do something with variables i.e construct new object then
//re-loop to construct new object with next set of data
}
的伎倆的線做一些事情是的ReadLine()被調用後,它會再向下移動一行在文本文件,然後下一個變量將被分配給下面的文字等等。我如何在C++中重新創建它?
「的std :: string的姓名,地址,年齡;」 'getline(myfile,name);' 'getline(myfile,address);' 'getline(myfile,age);' – 2014-09-24 22:40:22