我正在尝试将 fstream 传递给一个函数,然后将结构写入文件。我知道您必须通过引用传递流,但在运行时没有将任何内容写入文件。继承人我到目前为止:
struct Record
{
char name [16];
char phoneNum [16];
float balance;
};
void newRec (fstream &);
int main()
{
fstream ref;
ref.open("prog2.dat", ios::in | ios::out | ios::app | ios::binary);
if(!ref.fail() )
{
int choice = menu(ref);
system("CLS");
while(choice != 6)
{
choice = menu(ref);
system("CLS");
}
}
else
cout << "Error opening file. " << endl;
return 0;
}
void newRec (fstream& ref)
{
Record rec;
cout << "Enter customer name: ";
cin.ignore();
cin.getline(rec.name, sizeof(rec.name));
cout << "Enter customer phone number: ";
cin >> rec.phoneNum;
cout << "Enter beginning account balance: ";
cin >> rec.balance;
ref.write(reinterpret_cast<char*>(&rec), sizeof(rec));
}
rec 只是一个 3 成员结构。任何想法为什么这不起作用?我很感激任何帮助。注意:根据我的任务,我必须使用 .write() 而不是 <<