C++ Primer 5th 第8章 IO库

IO类对象不允许进行拷贝操作。

IO类中定义后一些函数和标志,可以用于访问和操作流的状态。

一旦流发生错误,后续IO操作都是失败的。

读写IO对象会改变IO对象的状态。

每个输出流都管理一个缓冲区。

缓冲被刷新的原因有如下几种:

1.程序正常结束。

2.缓冲区满

3.endl刷新缓冲区

4.使用操作符unitbuf

5.被关联到另一个流

 

输入或输出流可以被关联到另一个输出流,不允许关联到输入流。

每个流最多同时关联到一个流,一个流可以被多个流关联。

 

文件流是将抽象流与文件关联起来,对流的操作就能等同于对文件的操作了。

每个文件流都有默认的文件模式。

 

string流是将对string的操作等同于对内存IO操作,而不是具体string。

 

 

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

istream& fun(istream &input)
{
    string s;
    while (true)
    {
        input >> s;
        if (input.eof())
        {
            cout << s << '\n';
            break;
        }
        input.clear();        //防止fail和bad
        cout << s << '\t';
    }
    input.clear();
    return input;
}

 

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

#include <iostream>

using namespace std;

istream& fun(istream &input)
{
    string s;
    while (true)
    {
        input >> s;
        if (input.eof())
            break;
        else
            input.clear();
        cout << s << '\t';
    }
    input.clear();
    return input;
}

int main()
{
    fun(cin);
    return 0;
}

 

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

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

当cin流bad、fail、eof时循环终止

 

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

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

using namespace std;

int main(int argc, char const *argv[])
{
    vector<string> v;
    ifstream in_file(argv[1]);
    if (in_file)
    {
        string s;
        while (getline(in_file,s))
        {
            v.push_back(s);
        }
    }
    else
    {
        cout << "no file?!" << endl;
    }
    return 0;
}

 

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

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

using namespace std;

int main(int argc, char const *argv[])
{
    vector<string> v;
    ifstream in_file(argv[1]);
    if (in_file)
    {
        string s;
        while (in_file >> s)
        {
            v.push_back(s);
        }
    }
    else
    {
        cout << "no file?!" << endl;
    }

    for (auto i : v)
    {
        cout << i << ' ';
    }
    return 0;
}

 

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

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char const *argv[])
{
    ifstream input(argv[1]);
    Sales_data total;
    if (input >> total)
    {
        Sales_data trans;
        while ((input >> total))
        {
            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>

using namespace std;

int main(int argc, char const *argv[])
{
    ifstream input(argv[1]);
    ofstream output(argv[2]);
    Sales_data total;
    if (input >> total)
    {
        Sales_data trans;
        while ((input >> total))
        {
            if (total.isbn() == trans.isbn())
                total.combine(trans);
            else
            {
                output << total << endl;
                total = trans;
            }
        }
        output << total << endl;
    }
    else
    {
        cerr << "No data?!" << endl;
    }
    return 0;
}

 

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

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char const *argv[])
{
    ifstream input(argv[1]);
    ofstream output(argv[2], ofstream::app);
    Sales_data total;
    if (input >> total)
    {
        Sales_data trans;
        while ((input >> total))
        {
            if (total.isbn() == trans.isbn())
                total.combine(trans);
            else
            {
                output << total << endl;
                total = trans;
            }
        }
        output << total << endl;
    }
    else
    {
        cerr << "No data?!" << endl;
    }
    return 0;
}

 

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

#include <iostream>
#include <sstream>

using namespace std;

istream& fun(istream& input)
{
    string s;
    while (true)
    {
        input >> s;
        if (input.eof())
        {
            cout << s << '\n';
            break;
        }
        input.clear();        //防止fail和bad
        cout << s << '\t';
    }
    input.clear();
    return input;
}

int main(int argc, char const *argv[])
{
    istringstream is("hello");
    fun(is);
    return 0;
}

 

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

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

using namespace std;

int main(int argc, char const *argv[])
{
    vector<string> v;
    string line, one;
    ifstream in_file(argv[1]);
    if (in_file)
    {
        while (getline(in_file, line))
        {
            //cout << line << endl;
            v.push_back(line);
        }
    }

    for (auto i : v)
    {
        istringstream in_string(i);
        //cout << i << endl;
        while (in_string >> one)
        {
            cout << one << ' ';
        }
        cout << endl;
    }
    cout << endl;
    return 0;
}

 

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

#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

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

int main(int argc, char const *argv[])
{
    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中使用类内初始化?
因为不需要类内初始化,PersonInfo的数据成员将默认初始化。

 

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

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

using namespace std;

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

string format(string)
{
    return "format";
}

bool valid(string)
{
    int i;
    return i % 2;
}

int main(int argc, char const * argv[])
{
    ifstream in_file("~/people");
    if (!in_file)
    {
        cerr << "no file?!" << endl;
        return 1;
    }
    string line, word;
    vector<PersonInfo> people;
    while (getline(in_file, 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& ?
一是避免拷贝,二是防止改变它们。

转载于:https://www.cnblogs.com/pluse/p/5133624.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值