c++ primer 学习笔记9--文件输入输出

书285页,练习8.4

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

int main()
{
	ifstream in("data.txt");
	if (!in)
	{
		cerr << "无法打开输入文件" << endl;
		return -1;
	}
	string line;
	vector<string> words;
	while (getline(in, line))
	{
		words.push_back(line);
	}
	in.close();
	vector<string>::const_iterator it = words.begin();
	while (it != words.end())
	{
		cout << *it << " ";
		++it;
	}
}

8.6+8.7将输入文件名作为第一个参数,传给main函数,将输出文件名作为第二个参数传给main函数。

#include <iostream>  
#include <fstream>
#include "Sales_data.h"
using namespace std;

int main(int argc,char *argv[])
{
	if(argc != 3)
	{
		cerr << "请给出输入,输出文件名" << endl;
		return -1;
	}
	ifstream in(argv[1]);
	if (!in)
	{
		cerr << "无法打开输入文件" << endl;
		return -1;
	}
	ofstream out(argv[2]);
	if (!out)
	{
		cerr << "无法打开输出文件" << endl;
		return -1;
	}
	Sales_data total;
	if (read(in, total))
	{
		Sales_data trans;
		while (read(out, trans))
		{
			if (total.isbn() == trans.isbn())
				total.combine(trans);
			else
			{
				print(out, total) << endl;
				total = trans;
			}
		}
		print(out, total) << endl;
	}
	else
		cerr << "No data" << endl;


}
练习8.10,将来自一个文件中的行保存在一个vector<string>中,然后使用一个istringstream从vector读取数据元素,每次读取一个单词

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

int main(int argc,char *argv[])
{
	ifstream in("data.txt");
	if (!in)
	{
		cerr << "无法打开输入文件" << endl;
		return -1;
	}
	string line;
	vector<string> words;
	while (getline(in, line))
	{
		words.push_back(line);

	}
	in.close();
	vector<string>::const_iterator it = words.begin();
	while (it != words.end())
	{
		istringstream line_str(*it);
		string word;
		while (line_str >> word)
		{
			cout << word << " ";
			++it;
		}
	}


}

8.11

#include <iostream>  
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
struct PersonInfo
{
	string name;
	vector<string> phones;
};

int main(int argc,char *argv[])
{
	string line, word;
	vector<PersonInfo> people;
	istringstream record;
	while (getline(cin, line))
	{
		PersonInfo info;//创建一个保存此数据的对象
		record.clear();//重复使用字符串流的时候,每次调用clear
		record.str(line);//将记录绑定到刚刚读入的行
		record >> info.name;//读取名字
		while (record >> word)//保持
		{
			info.phones.push_back(word);
		}
		people.push_back(info);
	}


}
8.13

#include <iostream>  
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
struct PersonInfo
{
	string name;
	vector<string> phones;
};
string format(const string &s){ return s; }
bool valid(const string &s)
{
	//
	//
	return true;
}

int main(int argc,char *argv[])
{
	string line, word;
	vector<PersonInfo> people;
	istringstream record;
	if (argc != 2)
	{
		cerr << "请给出文件名" << endl;
		return -1;
	}
	ifstream in(argv[1]);
	if (!in)
	{
		cerr << "无法打开输入文件" << endl;
		return -1;
	}

	while (getline(cin, line))
	{
		PersonInfo info;//创建一个保存此数据的对象
		record.clear();//重复使用字符串流的时候,每次调用clear
		record.str(line);//将记录绑定到刚刚读入的行
		record >> info.name;//读取名字
		while (record >> word)//保持
		{
			info.phones.push_back(word);
		}
		people.push_back(info);
	}
	ostringstream os;
	for (const auto &entry : people)
	{
		ostringstream formatted, badNums;
		for (const auto &nums : entry.phones)
		{
			if (!valid(nums))
			{
				badNums << " " << nums;
			}
			else
			{
				formatted << " " << format(nums);
			}
			if (badNums.str().empty())
			{
				os << entry.name << " " << formatted.str() << endl;
			}
			else
			{
				cerr << "input error:" << entry.name << "invalid number(s)" << badNums.str() << endl;
			}
			cout << os.str() << endl;
		}
	}

}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值