如何在现有文本文件的末尾附加一些文本

How to append some text at the end of an existing text file

本文关键字:文本 文件      更新时间:2023-10-16

如果可能的话,任何帮助都很好

我已经尝试了此代码

float deposit (float balance)
{

     double amount; 
    system("cls");
    cout<<"Enter the amount you wish to deposit"<<endl; 
    cin>>amount; 

    ofstream newBalance;                        
    newBalance.open ("deposit.txt", fstream::app);
    newBalance<<amount; 
    newBalance.close();
    balance = balance + amount; 
    writeBalance(balance); 
    return balance;  
}
//This is a function to allow the user to increase their balance 

您可能要放置

fstream::out | fstream::app

而不是fstream::app

您可以查看:

http://www.cplusplus.com/reference/fstream/fstream/open/

放入新行只做:

newBalance<<amount<<"n";