【发布时间】:2014-04-23 20:00:14
【问题描述】:
我正在尝试在文件上写一些东西。但它只是没有像它必须假设的那样写作。
void main()
{
int accno;
char name[20];
float deposit;
clrscr();
ofstream writefile("icici.txt");
cout<<"enter your saving bank account number";
cin >> accno;
writefile << name << endl;
cout<<"\n\nYour good name:";
cin >> name;
writefile << name << endl;
cout<<"\n\nKey in recent deposit in Rs:";
cin >> accno;
writefile << deposit << endl;
writefile.close();
cout << "\n\nFile is created successfully...\n\n" << endl;
ifstream readfile("icici.txt");
readfile >> accno;
readfile >> name;
readfile >> deposit;
cout<<"\nContent of file is icici.txt is read as follows:\n";
cout<<"\nSaving bank account number:"<<accno<<endl;
cout<<"\nCustomer name smt/shri:"<<name<<endl;
cout<<"\nDeposit amount in Rs:"<<deposit<<endl;
getch();
}
它在文件中写的是这样的:
99
Mak
3.780703e-42
我做错了什么?
【问题讨论】:
-
你想做什么?预期的结果是什么?
-
打开编译器警告并仔细查看提示输入存款金额后的行。
-
第 14 行,
writefile << name << endl;名称未初始化。 -
cin >> accno;出现 2 次。你想要cin >> deposit第二次。 -
天啊,我真的很抱歉我的坏! :D