这里涉及到了写入文本的操作,这是进一步学习的基础,这里大家要好好学习
其实输入到文本和输入到显示器显示是一个原理,只是输出的地方不一样
这样能输出了,能做的事情就更多了.比如写一个给51,32单片机配置一下就把程序写出来的程序
小程序做漂亮了.会给个人带来很多意想不到的东西.
#pragma region cingoif.cpp---程序清单6.15
//程序清单6.15
//outfile.cpp -- writing to a file
#if 1
#include <iostream>
#include<fstream> //for file I/O
int main()
{
using namespace std;
char automobile[50];
int year;
double a_price;
double d_price;
ofstream outFile; //create object for output
outFile.open("Carinfo.txt"); //associate with a file
cout << "输入汽车的品牌和型号:";
cin.getline(automobile, 50);
cout << "输入车型年份:";
cin >> year;
cout <<"输入历史价格:";
cin >> a_price;
d_price = 0.913 * a_price;
//用cout在屏幕上显示信息
cout << fixed;
cout.precision(2);
cout.setf(ios_base::showpoint);
cout << "品牌和型号:" << automobile << endl;
cout << "年份: " << year << endl;
cout << "历史价格 " << a_price << endl;
cout << "当前价格 " << d_price << endl;
//现在用outFile代替cout做完全相同的事情
outFile << fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
outFile << "品牌和型号:" << automobile << endl;
outFile << "年份: " << year << endl;
outFile << "历史价格 " << a_price << endl;
outFile << "当前价格 " << d_price << endl;
outFile.close();//done with file
return 0;
}
#endif
#pragma endregion
只需要把cout,改为outFile(ofstream对象)