我对 C++ 很陌生,正在尝试研究如何以下面这种结构的格式将记录写入文本文件:
struct user {
int id;
char username [20];
char password [20];
char name [20];
char email [30];
int telephone;
char address [70];
int level;
};
到目前为止,我可以很好地写入它,但没有递增的 id 编号,因为我不知道如何计算记录数,因此在我将数据写入文件后文件看起来像这样。
1 Nick pass Nick email tele address 1
1 user pass name email tele address 1
1 test test test test test test 1
1 user pass Nick email tele addy 1
1 nbao pass Nick email tele 207 1
使用以下代码:
ofstream outFile;
outFile.open("users.dat", ios::app);
// User input of data here
outFile << "\n" << 1 << " " << username << " " << password << " " << name << " "
<< email << " " << telephone << " " << address << " " << 1;
cout << "\nUser added successfully\n\n";
outFile.close();
那么,如何在插入时增加每条记录的值,然后如何定位文件中的特定记录?
编辑:我已经能够显示每一行:
if (inFile.is_open())
{
while(!inFile.eof())
{
cout<<endl;
getline(inFile,line);
cout<<line<<endl;
}
inFile.close();
}