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