繁体   English   中英

C ++-写入文件时遇到麻烦(流)

[英]c++ - having trouble with writing in the file (ofstream)

我正在尝试在文件上写一些东西。 但是它只是没有像它必须的那样写作。

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<<endlname字段未定义。 可能是您想写accno而不是name吗?

您正在使用不正确的变量名称,并在读取和初始化变量之前使用它们。

这行写文件writefile << name << endl; 需要跟随cin >> name; 在您的代码中,它将继续进行处理,这意味着名称不包含任何内容或垃圾数据。

我认为这些线

cout<<"\n\nKey in recent deposit in Rs:";
cin >> accno;

应该:

cout<<"\n\nKey in recent deposit in Rs:";
cin >> deposit;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM