尝试使用while循环读取一个文件(它被称为“Stocks.dat”,我之所以使用while循环是因为将来可以追加X数量的股票;下面列出.dat文件的格式),以创建第二个文件“profits.dat”,其中包含“Stocks.dat”的信息,但带有新的计算浮点数。这运行罚款,但是当我检查“profits.dat”是否存在,那里什么也没有。为什么我没有得到一个包含所有计算内容的“profits.dat”文件?非常感谢。在一个.dat文件中无法读取以创建第二个.dat文件
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char name[40];
int sh;
float pp, cp, pc, cv, pr;
ifstream infile("c:\\Stocks.dat", ios::in);
ofstream outfile("c:\\profits.dat", ios::out);
while (infile.getline(name, 40)) //don't forget the whitespace
{
infile >> sh >> pp >> cp;
pc = sh * pp;
cv = sh * cp;
pr = cv - pc;
outfile << name << endl; //need the endline
outfile << pc << ' ' << cv << ' ' << pr << ' ' << endl; //need the blank spaces
infile.ignore(40, '\n');
}
infile.close();
outfile.close();
return 0;
}
//Stocks.dat information:
//APPLE
//500 20.25 30.75
//MICROSOFT
//250 9.75 12.99
//GOOGLE
//1000 50.10 300.85
'outfile.fail()== false'?可能没有权限在'C:'上编写。尝试桌面。按预期在我的机器上工作,但路径更改为桌面 – mpiatek
不确定你在说什么?我会如何给C写权限?我以前制作过读/写程序,并将其保存在项目文件夹中,但“profits.dat”不在项目文件夹中。 “尝试桌面”是什么意思? – polymorphism
尝试改变桌面和没有骰子的路径:( – polymorphism