[英]c++ - having trouble with writing in the file (ofstream)
I am trying to write something on the file. 我正在尝试在文件上写一些东西。 But its just not writing as it must suppose to.
但是它只是没有像它必须的那样写作。
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();
}
And it writes in the file is like so: 它写在文件中就像这样:
99
Mak
3.780703e-42
What am I doing wrong? 我究竟做错了什么?
At the first writefile<<name<<endl
, name
field is undefined. 在第一个
writefile<<name<<endl
, name
字段未定义。 May be would you like to write accno
instead of name
? 可能是您想写
accno
而不是name
吗?
You are using the incorrect variable names and using variables before they've been read and initialized. 您正在使用不正确的变量名称,并在读取和初始化变量之前使用它们。
This line writefile << name << endl;
这行写文件
writefile << name << endl;
needs to follow cin >> name;
需要跟随
cin >> name;
In your code, it proceeds it and that means name doesn't contain anything or maybe junk data. 在您的代码中,它将继续进行处理,这意味着名称不包含任何内容或垃圾数据。
I think these lines 我认为这些线
cout<<"\n\nKey in recent deposit in Rs:";
cin >> accno;
Should be: 应该:
cout<<"\n\nKey in recent deposit in Rs:";
cin >> deposit;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.