-2

我正在开发一个 C++ 银行系统。我能够正确获取浮点、newbal 值,当我尝试写入文件时,文件中没有数据。

else if (x == 2)
{
    cout << "You have selected option number 2. Deposit.\n";
    cout << "Please enter you account ID: ";
    cin >> ID;
    file.open("C:\\Users\\Raggulddon\\Desktop\\C++ supplement\\Cust_" + ID + ".dat", ios:: in | ios::out | ios::binary);

    if (!file)
    {
        cout << "Sorry the requested account could not be located.\n";
    }
    else
    {
        file >> firstname >> lastname;
        cout << endl << firstname << " " << lastname << endl;
        cout << "-----------------------------------\n";
        string line;
        while (getline(file, line))
        {

            // stringstream the getline for line string in file
            istringstream iss(line);

            if (iss >> date >> amount)
            {
                cout << date << "\t\t$" << showpoint << fixed << setprecision(2) << amount << endl;
                famount += amount;
            }

        }
        cout << "Your balance is $" << famount << endl;
        cout << "How much would you like to deposit today: $";
        cin >> amountinput;

        float newbal = 0;
        newbal = (famount += amountinput);


        cout << "\nYour new balance is: $" << newbal << ".\n";
        file << date << "\t\t" << newbal; //***This should be writing to file
        but it doesn 't.
            
file.close();

文本文件如下所示:

托尼·加迪斯

12 年 5 月 24 日 100

12 年 5 月 30 日 300

2012 年 7 月 1 日 -300

// 控制台输出看起来像这样

托尼·加迪斯

12 年 5 月 24 日 100

12 年 5 月 30 日 300

2012 年 7 月 1 日 -300

您的余额为:#1

你想存多少钱:#2

您的新余额为:#1 + #2

写入文件

关闭文件。

// 退出主循环::::

我怎样才能让它写入文件并保存它,为什么会这样。

ostringstream考虑到我如何用于istringstream输入,我也尝试过这样做。但它也没有工作:

float newbal=0;
newbal = (famount += amountinput);

ostringstream oss(newbal);
oss << date << "\t\t" << newbal;

我正在尝试自学 C++,因此任何相关信息将不胜感激。

4

1 回答 1

0

如果要编写文本文件,则在打开文件时不应使用“ios::binary”。

于 2013-10-31T12:28:36.557 回答