我正在使用c ++在clr windows窗体中创建一个项目,并且我在加载表单时在模板(向量)中从文件中检索数据时遇到问题。我的表单有一个按钮来保存代码,该代码在如下编码的类中调用save方法。请提示检索文件的代码应该是什么,谢谢。其中记录是向量的名称。
int men_database::save(int count)
{ ofstream out;
out.open("MALE.txt",ios::out|ios::binary);
if(!out)
return -1;
else
{for(int i=0;i<count;i++)
{out<<'\n'<<record[i].getid();
out<<'\n'<<record[i].getname();
out<<'\n'<<record[i].getsize();
out<<'\n'<<record[i].getcolor();
out<<'\n'<<record[i].getprice();
out<<'\n'<<record[i].getpic();
out<<'\n'<<record[i].getwatch();
}
out.close();
}//else ends
return 1;}
答案 0 :(得分:0)
以下是一些读/写的基本代码。虽然你可能会在别处找到更好的东西\n
用作分隔符。每条记录应该有2个字段(在你的字段中是7个字段)。每个字段必须是单行字符串。 Ps,你在问题上用c ++标签得到更多关注。
int main()
{
{
ofstream f("data.txt");
f << "id1" << endl;
f << "name1" << endl;
f << endl;
f << "id2" << endl;
f << "name2" << endl;
f << endl;
}
{
ifstream f("data.txt");
string id;
string name;
while (f)
{
f >> id;
f >> name;
if (!f) break;
cout << "id: " << id << endl;
cout << "name: " << name << endl;
cout << endl;
}
}
return 0;
}