【发布时间】:2013-10-31 03:49:41
【问题描述】:
我正在开发一个 C++ 银行系统。 我能够正确获取 float、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
05/30/12 300
07/01/12 -300
// 控制台输出如下所示
托尼·加迪斯
12 年 5 月 24 日 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++,因此任何相关信息都将不胜感激。
【问题讨论】:
-
您的具体问题是什么?
-
在读写时检查错误是有原因的。当您尝试写入时,可能会在文件上设置 eof 标志,因为您从中读取了所有内容。
-
我的问题是:为什么这不会写入文件 file
标签: c++ string loops fstream sstream