gpt4 book ai didi

c++ - 写入文件时遇到问题(ofstream)

转载 作者:行者123 更新时间:2023-11-28 06:55:20 26 4
gpt4 key购买 nike

我想在文件上写点东西。但它只是没有像它必须假设的那样编写。

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

我做错了什么?

最佳答案

首先writefile<<name<<endl , name字段未定义。也许你想写accno而不是 name

关于c++ - 写入文件时遇到问题(ofstream),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23254081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com