1.学习了文件输出后,看了c++ primer plus的例6.15.,有些不懂的注释出来,便于以后学习。
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char automobile[50];
int year;
double a_price;
double b_price;
ofstream outFile;
outFile.open("E:\\grade2017.txt",ios::out); //和书上不同,规定了输出文件的路径
cout<<"enter the make and model of automobile"<<endl;
cin.getline(automobile,50);
cout<<"enter the model year"<<endl;
cin>>year;
cout<<"enter the original asking price:";
cin>>a_price;
b_price=a_price*0.9;
//利用cout在屏幕上显示出结果
cout<<fixed;
cout.precision(2);
cout.setf(ios_base::showpoint);
cout<<"make and model:"<<automobile<<endl;
cout<<"year:"<<year<<endl;
cout<<"was asking price:"<<a_price<<endl;
cout<<"Now asking price:"<<b_price<<endl;
//利用outFile输出文件
outFile<<fixed; //用一般形式输出字浮点数
outFile.precision(2);// 表示两位小数点输出
outFile.setf(ios_base::showpoint); //强制显示小数点输出
outFile<<"make and model:"<<automobile<<endl;
outFile<<"year:"<<year<<endl;
outFile<<"was asking price:"<<a_price<<endl;
outFile<<"Now asking price:"<<b_price<<endl;
//切记关闭文件
outFile.close();
return 0;
}
2.学习cout.self 和cout.precision()
这两个就是格式控制的~ostream成员函数里面的,也可以用输出流操作符来控制,都一样的~附给你一些看看~