代码是这样的
ofstream f("bank.dat", ios::app);
ifstream fa("bank.dat");
int n = 0, flag = 0;
struct bac
{
char name[10];
char amt[5];
} s;
void add()
{
cout << "\nenter the details ";
cin >> s.name >> s.amt;
f.write((char *)&s, sizeof(bac));
}
void ser()
{
ifstream fa("bank.dat");
fa.seekg(0);
char x[10];
cout << "\nenter value to be searched ";
cin >> x;
while (fa && flag == 0)
{
n++;
fa.read((char *)&s, sizeof(bac));
if (strcmp(s.name, x) == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout << "\nfound";
cout << "\nAmount " << s.amt;
}
}
void mod()
{
ser();
cout<<" "<<n;
if (flag == 1)
{
f.seekp((n - 1) * sizeof(bac));
// cout<<f.tellp();
cout<<"\nnew details ";
add();
}
}
int main()
{f.seekp(0);
int ch;
cout << "\nBANK MANAGEMENT SYSTEM \n";
cout << "enter choice ";
cout << "\n1.add\n2.search\n3.delete and overwrite ";
cin >> ch;
if (ch == 1)
{
add();
}
if (ch == 2)
{
ser();
}
if (ch == 3)
{
mod();
}
return 0;
}
我想做的是制作一个具有搜索、显示和修改功能的程序;
错误
记录最后被附加,即使我使用
f.seekp((n - 1) * sizeof(bac));
执行的操作
*添加 sid , sar 分别以 amts 5,6 命名的条目
*用名称替换sid命名条目:sid(与原始相同)amt:7
文件中的输出
预计 sid 7 sar 6
找到 sid 5 sar 6 sid 7