我正在开发一个C ++银行系统。 我能够正确地获得浮动,新的,值,当我尝试写入文件时,文件中没有数据。
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();
文本文件如下所示:
Tony Gaddis
05/24/12 100
05/30/12 300
07/01/12 -300
//控制台输出如下所示
Tony Gaddis
05/24/12 100
05/30/12 300
07/01/12 -300
您的余额是:#1
您愿意存多少钱:#2
您的新余额是:#1 +#2
写入文件
关闭文件。
//退出主循环::::
如何将其写入文件并保存,以及为什么会发生这种情况。
我尝试使用ostringstream
并考虑如何使用istringstream
作为输入。但它也没有工作:
float newbal=0;
newbal = (famount += amountinput);
ostringstream oss(newbal);
oss << date << "\t\t" << newbal;
我正在尝试自学C ++,所以任何相关的信息都会受到赞赏。
答案 0 :(得分:0)
如果要编写文本文件,打开文件时不应使用“ios :: binary”。