【发布时间】:2013-01-10 19:02:00
【问题描述】:
我有一个文本文件“test.txt”,它存储了我的数据如下,每个分隔符字段之间有一个空格..
代码:姓名:腼腆
045: Ted: Coy1
054: Red: Coy2
如何从文件中读取这些数据并将其插入向量中?
vector <Machine> data;
Machine machine
void testclass(){
ifstream inFile("test.txt");
if (!inFile){
cout << "File couldn't be opened." << endl;
return;
}
while(!inFile.eof()){
string code,name,coy;
getline(inFile,code, ':');
getline(inFile,name, ':');
getline(inFile,coy, ':');
data.push_back(machine)
}
但是推送数据好像有问题
【问题讨论】:
-
while(!inFile.eof())is wrong here。哪本书指示你这样做?
标签: c++ vector ifstream getline