C++ Primer: Chapter 8 IO Library

Homework

Test 8.5

int main(int argc, char *argv[]) 
{
	vector<string> svec;
	readword(argv[1], svec);
	
	for (auto s : svec)
	{
		cout << s << endl;
	}
}

Test 8.6 & 8.7

int main(int argc, char *argv[]) {

	ifstream input(argv[1]);
	ofstream output(argv[2]);
	my_sales_data total;
	if (read(input, total))
	{
		my_sales_data trans;
		while (read(input, trans))
		{
			if (total.isbn() == trans.isbn())
				total.combine(trans);
			else
			{
				print(output, total) << endl;
				total = trans;
			}
		}
		print(output, total) << endl;
	}
	else
		cerr << "No data?" << endl;

    return 0;
}

Test 8.8

在main.cpp中:

int main(int argc, char *argv[]) {

	ifstream input(argv[1]);
	ofstream output(argv[2], ofstream::app);
	my_sales_data total;
	if (read(input, total))
	{
		my_sales_data trans;
		while (read(input, trans))
		{
			if (total.isbn() == trans.isbn())
				total.combine(trans);
			else
			{
				print(output, total) << endl;
				total = trans;
			}
		}
		print(output, total) << endl;
	}
	else
		cerr << "No data?" << endl;

    return 0;
}

在cmd中:

>cl main.cpp my_sales_data.cpp
>main.exe infile.txt outfile.txt

Test 8.10

int main(int argc, char *argv[]) 
{
	ifstream ifs(argv[1]);
	vector<string> lvec;
	string line;
	while (getline(ifs, line))
	{
		lvec.push_back(line);
	}

	istringstream istr;
	for (auto i : lvec)
		istr >> i;

    return 0;
}

Test 8.13

struct PersonInfo
{
	string name;
	vector<string> phones;
};
int main(int argc, char *argv[]) 
{
	string line, word;
	vector<PersonInfo> people;
	
	ifstream ifs(argv[1]);
	while (getline(ifs, line))
	{
		PersonInfo info;
		istringstream record(line);
		record >> info.name;
		while (record >> word)
			info.phones.push_back(word);
		people.push_back(info);
	}

	for (const auto& p : people)
	{
		cout << p.name <<endl;
		for (const auto& i : p.phones)
			cout << i << endl;
		cout << endl;
	}
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值