C++ Primer第八章 IO类

8.1 IO类

8.1.1 IO对象无拷贝或赋值

8.1.2 条件状态

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

#include<iostream>
#include<string>

std::istream &print_is(std::istream &is)
{
    std::string s;
    while (is >> s)
    {
        std::cout << s << std::endl;
    }
    is.clear();
}

int main()
{
    print_is(std::cin);
    return 0;
}

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

while (cin >> i) /*  ...    */
//badbit、failbit和eofbit任一个被置位,则检测流状态的条件会失败。

8.1.3 管理输出缓冲

8.2 文件输入输出

8.2.1 使用文件流对象

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

#include <fstream>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
	ifstream ifs("data");
	vector<string> v1;
	string buf;

	if(ifs)
	{
		while(getline(ifs,buf))
		{
			v1.push_back(buf);
		}
	}


	for(const auto &s : v1)
		cout << s << endl;

	return 0;
}

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

#include <fstream>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
	ifstream ifs("data");
	vector<string> v1;
	string buf;

	if(ifs)
	{
		while(ifs >> buf)
		{
			v1.push_back(buf);
		}
	}


	for(const auto &s : v1)
		cout << s << endl;

	return 0;
}

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

#include <iostream>
#include<fstream>
#include "Sales_data.h"

int main(int argc, char *argv[])
{
    std::ifstream input(argv[1]);
    //std::ofstream output(argv[2]);
    Sales_data total;

    if (read(input, total))
    {
        Sales_data trans;
        while (read(input, trans))
        {
            // 读取并处理剩余的交易记录
            if (total.isbn() == trans.isbn())
                total = add(total, trans); // 更新总销售额
            else
            {
                print(std::cout, total);
                cout << endl;  // 打印前本书结果
                total = trans; // total表示当前这本书
            }
        }
        print(std::cout, total);
        cout << endl;
    }
    else
        std::cerr << "No data!" << endl;
    return 0;
}

8.2.2 文件模式

8.7 修改上一节的书店程序,将结果保存到一个文件中。将输出文件名作为第二个参数传递给main函数。
8.8 修改上一题的程序,将结果追加到给定的文件末尾。对同一个输出文件,运行程序至少两次,检验数据是否得以保留。

#include <iostream>
#include<fstream>
#include "Sales_data.h"

int main(int argc, char *argv[])
{
    std::ifstream input(argv[1]);
    std::ofstream output(argv[2], std::ofstream::app);
    Sales_data total;

    if (read(input, total))
    {
        Sales_data trans;
        while (read(input, trans))
        {
            // 读取并处理剩余的交易记录
            if (total.isbn() == trans.isbn())
                total = add(total, trans); // 更新总销售额
            else
            {
                print(output, total);
                cout << endl;  // 打印前本书结果
                total = trans; // total表示当前这本书
            }
        }
        print(output, total);
        cout << endl;
    }
    else
        std::cerr << "No data!" << endl;
    return 0;
}

8.3 string流

8.3.1 使用istringstream

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

#include<iostream>
#include<string>
#include<sstream>

std::istream &print_is(std::istream &is)
{
    std::string s;
    while (is >> s)
    {
        std::cout << s << std::endl;
    }
    is.clear();
}

int main()
{
    std::istringstream is("abc");
    print_is(is);
    return 0;
}

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

#include <iostream>
#include <fstream>
#include <string>
#include<vector>
#include<sstream>

using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::string;
using std::vector;
using std::istringstream;

int main()
{
    // 1 文件中的行保存在一个vector<string>中
    string line, word;
    vector<string> vec;
    ifstream in("file1");
    while (getline(in, line))
        vec.push_back(line);
    for(auto s : vec)
        cout << s << endl;

    // 2 使用istringstream从vector读取数据,每次读取一个单词
    for(auto s : vec)
    {
        istringstream record(s);
        while (record >> word)
        {
            cout << word << endl;
        }
        
    }
        
    return 0;
}

8.11 本节的程序在外层while循环中定义了istringstream 对象。如果record 对象定义在循环之外,你需要对程序进行怎样的修改?重写程序,将record的定义移到while 循环之外,验证你设想的修改方法是否正确。

#include <string>
#include <vector>
#include <iostream>
#include<sstream>
using std::cin;
using std::string;
using std::vector;
using std::istringstream;

struct PersonInfo
{
    string name;
    vector<string> phones;
};

int main()
{
    string line, word;
    vector<PersonInfo> people;

    istringstream record;
    while (getline(cin, line))
    {
        PersonInfo info;
        record.str(line);
        record >> info.name;
        while (record >> word)
            info.phones.push_back(word);
        people.push_back(info);        
    }
    return 0;
}

8.12 我们为什么没有在PersonInfo中使用类内初始化?

string和vector可以自己初始化,并不会出现未定义的情况。

8.3.2 使用ostringstream

8.13 重写本节的电话号码程序,从一个命名文件而非cin读取数据。

#include <string>
#include <vector>
#include <iostream>
#include<sstream>
#include<fstream>
using std::cin;
using std::cout;
using std::endl;
using std::cerr;
using std::string;
using std::vector;
using std::istringstream;
using std::ostringstream;
using std::ifstream;

struct PersonInfo
{
    string name;
    vector<string> phones;
};

bool valid(const string &s)
{
    for(auto c : s)
        if(!isdigit(c))
            return false;
    return true;
}

string format(const string &s)
{
    return s;
}

int main()
{
    string line, word;
    ifstream in("file1");
    vector<PersonInfo> people;

    while (getline(in, line))
    {
        PersonInfo info;
        istringstream record(line);
        record >> info.name;
        while (record >> word)
            info.phones.push_back(word);
        people.push_back(info);        
    }

    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())
            cout << entry.name << " " << formatted.str() << endl;
        else
            cerr << "input error: " << entry.name << " invalid number(s) " << badNums.str() << endl;
    }
    return 0;
}

8.14 我们为什么将entry和nums定义为 const auto& ?

它们都是类类型,因此使用引用避免拷贝,提高了效率;在循环当中不会改变它们的值,因此用const。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值