嘗試使用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