《C++ Primer》5th 课后练习 第八章 IO库 1-10

练习8.1 编写函数,接受一个istream&参数,返回值类型也是istream&。此函数须从给定流中读取数据,直至遇到文件结束标识时停止。它将读取的数据打印在标准输出上。完成这些操作后,在返回流之前,对流进行复位,使其处于有效状态。

istream &func(istream &in) {
	char c;
	while (in >> c) {
		cout << c;
	}
	in.clear();
	return in;
}

练习8.2 测试函数,调用参数为cin

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
istream &func(istream &in) {
	char c;
	while (in >> c) {
		cout << c;
	}
	in.clear();
	return in;
}

int main(int argc, char **argv)
{
	istream &testin = func(cin);
	cout << testin.rdstate() << endl;
	return 0;
}

练习8.3 什么情况下,下面的while循环会终止?

while (cin >> i) /*  ...    */

当发生io错误时,比如i是一个int型变量却输入一个char型数据。(failbit被置位)

当输入eof结束符时。(eofbit被置位,eof被置位)

当该输入流崩溃时。(badbit被置位,failbit被置位)

练习8.4 编写函数,以读模式打开一个文件,将其内容读入到一个string的vector中,将每一行作为一个独立的元素存于vector中。

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

int main(int argc, char **argv)
{
	vector<string> vec;
	ifstream fin("test.txt");
	string s;
    if(fin){
        while (getline(fin, s)) {
            vec.push_back(s);
        }
    }
	for (auto &item : vec) {
		cout << item << endl;
	}
	return 0;
}

练习8.5 重写上面的程序,将每个单词作为一个独立的元素进行存储。

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

int main(int argc, char **argv)
{
	vector<string> vec;
	ifstream fin("test.txt");
	string s;
	if (fin) {
		while (fin >> s) {
			vec.push_back(s);
		}
	}
	
	for (auto &item : vec) {
		cout << item << endl;
	}
	return 0;
}

练习8.6 重写7.1.1节的书店程序,从一个文件中读取交易记录。将文件名作为一个参数传递给main。

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "Sale_data.h"
using namespace std;

int main(int argc, char **argv)
{
	ifstream fin(argv[1]);
	Sales_data total;
	if (!fin) {
		cerr << "open error" << endl;
		return 0;
	}
	if (read(fin, total)) {
		Sales_data trans;
		while (read(fin, trans)) {
			if (total.isbn() == trans.isbn())
				total.combine(trans);
			else {
				print(cout, total) << endl;
				total = trans;
			}
		}
		print(cout, total) << endl;
	}
	else {
		cerr << "No data?!" << endl;
	}
	return 0;
}

练习8.7 修改上一节的书店程序,将结果保存到一个文件中。将输出文件名作为第二个参数传递给main函数。

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "Sale_data.h"
using namespace std;

int main(int argc, char **argv)
{
	ifstream fin(argv[1]);
	ofstream fout(argv[2]);
	Sales_data total;
	if (!fin) {
		cerr << "open error" << endl;
		return 0;
	}
	if (read(fin, total)) {
		Sales_data trans;
		while (read(fin, trans)) {
			if (total.isbn() == trans.isbn())
				total.combine(trans);
			else {
				print(fout, total) << endl;
				total = trans;
			}
		}
		print(fout, total) << endl;
	}
	else {
		cerr << "No data?!" << endl;
	}
	return 0;
}

练习8.8 修改上一题的程序,将结果追加到给定的文件末尾。对同一个输出文件,运行程序至少两次,检验数据是否得以保留。

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "Sale_data.h"
using namespace std;

int main(int argc, char **argv)
{
	ifstream fin(argv[1]);
	ofstream fout(argv[2], ofstream::out | ofstream::app);
	Sales_data total;
	if (!fin) {
		cerr << "open error" << endl;
		return 0;
	}
	if (read(fin, total)) {
		Sales_data trans;
		while (read(fin, trans)) {
			if (total.isbn() == trans.isbn())
				total.combine(trans);
			else {
				print(fout, total) << endl;
				total = trans;
			}
		}
		print(fout, total) << endl;
	}
	else {
		cerr << "No data?!" << endl;
	}
	return 0;
}

练习8.9 使用你为8.1.2节第一个练习所编写的函数打印一个istringstream对象的内容。

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "Sale_data.h"
using namespace std;

istream &func(istream &in) {
	char c;
	while (in >> c) {
		cout << c;
	}
	in.clear();
	return in;
}

int main(int argc, char **argv)
{
	string s("aaa bbb\n ccc");
	istringstream sin(s);
	func(sin);
	return 0;
}

练习8.10 编写程序,将来自一个文件中的行保存在一个vector中。然后使用一个istringstream从vector读取数据元素,每次读取一个单词。

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main(int argc, char **argv)
{
	string instr, outstr;
	vector<string> vec;
	string file = "./123.txt";
	/*这个地方很有意思,我用的IDE是VS2017,
 	 *用绝对路径就可以找到文件,而相对路径则不行
	 *原因是,IDE在运行.exe文件时改变了运行目录
	 *想用相对路径寻址的话得更改IDE设置,或者去Debug目录下直接双击.exe文件
	 */
	ifstream fin(file);
	//cout << argv[1] << endl;
	//cout << fin.good() << endl;
	//cout << cin.good() << endl;
	if (fin) {
		while (fin >> instr) {
			vec.push_back(instr);
		}
		for (auto &ss : vec) {
			istringstream sin(ss);
			sin >> outstr;
			cout << outstr << endl;
		}
	}
	system("pause");
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值