文件读写问题
int main()
{
struct Mobile{
char automobile[50];
int year;
double a_price;
double d_price;
};
int n;
cout < < "How many Mobilephone do you wish to catalog? ";
cin> > n;
cin.get();
Mobile *mp=new Mobile[n];
ofstream outFile;
outFile.open( "test.txt ");//如何在文件末尾追加信息?
for(int i=0;i <n;i++){
cout < < "# " < <i+1 < <endl;
cout < < "Enter the make and model of mobile: ";
cin.getline (mp[i].automobile,50);
cout < < "Enter the model year: ";
cin> > mp[i].year;
cout < < "Enter the original asking price: ";
cin> > mp[i].a_price;
mp[i].d_price=0.913*mp[i].a_price;
cin.get();
}
cout < <fixed;//这3段代码是什么意思?
cout.precision(2);
cout.setf(ios_base::showpoint);
for(i=0;i <n;i++){
cout < < "**************************************************************** " < <endl;
cout < < "Make and model: " < <mp[i].automobile < <endl;
cout < < "Year: " < <mp[i].year < <endl;
cout < < "Was asking $: " < <mp[i].a_price < <endl;
cout < < "Now asking $: " < <mp[i].d_price < <endl;
outFile < < "# " < <i+1 < <endl;
outFile < < "Make and model: " < <mp[i].automobile < <endl;
outFile < < "Year: " < <mp[i].year < <endl;
outFile < < "Was asking $ " < <mp[i].a_price < <endl;
outFile < < "Now asking $ " < <mp[i].d_price < <endl;
}
outFile.close ();
return 0;
}
[解决办法]
outFile.open( "test.txt ");//如何在文件末尾追加信息?
你指什么? outFile.open( "test.txt ",ios::app);吗? 或者ios::out ==
cout < <fixed;//这3段代码是什么意思?
cout.precision(2);
cout.setf(ios_base::showpoint);
fixed:使用定点表示输出实数值;
precision:控制小数点后的精度为2位;
setf:在原有的基础上追加设置,不改变原有设置
showpoint:追加小数点