C++Primer Plus第六章分支语句和逻辑运算:写入到文本文件中(ofstream对象)

这里涉及到了写入文本的操作,这是进一步学习的基础,这里大家要好好学习
其实输入到文本和输入到显示器显示是一个原理,只是输出的地方不一样
这样能输出了,能做的事情就更多了.比如写一个给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对象)

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值