1.包含iostream和fstream;
2.创建一个ofstream的对象;
ofstream outFile;
3.将这个ofstream对象和一个文件关联起来;
outFile.open("carinfo.txt");
4.像使用cout一样使用ofstream对象。
outFile<<fixed;//fixed是用一般的浮点数格式输出,scientific是用科学计数法输出 outFile.precision(2);//小数点后两位 outFile.setf(ios_base::showpoint);//显示末尾的小数点 outFile<<"制造商:"<<automobile<<endl; outFile<<"生产年份:"<<year<<endl; outFile<<"原价:"<<a_price<<endl; outFile<<"现价"<<d_price<<endl;
5.关闭该对象
outFile.close();
浙公网安备 33010602011771号