Visual c++ Visual C++中的文件读取 我用C++在CLR Windows窗体中做了一个项目,IM在加载表单时从一个模板中的文件中检索数据时遇到了问题。我的表单有一个保存代码的按钮,该按钮在下面编码的类中调用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;}
下面是一些读/写的基本代码。尽管你可能会在别处找到更好的\n用作分隔符。每个记录中应有2个字段,即7个字段。每个字段必须是单行字符串。PS,你在问题上用C++标签得到更多的关注。Visual c++ Visual C++中的文件读取 我用C++在CLR Windows窗体中做了一个项目,IM在加载表单时从一个模板中的文件中检索数据时遇到了问题。我的表单有一个保存代码的按钮,该按钮在下面编码的类中调用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;},visual-c++,Visual C++,下面是一些读/写的基本代码。尽管你可能会在别处找到更好的\n用作分隔符。每个记录中应有2个字段,即7个字段。每个字段必须是单行字符串。PS,你在问题上用C++标签得到更多的关注。 int main() { { ofstream f("data.txt"); f << "id1" << endl; f << "name1" << endl; f << endl;
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;
}