C Primer

 主页   资讯   文章   代码   电子书 

Chapter 8. The IO Library

Exercise 8.1:

Write a function that takes and returns an istream&. The function should read the stream until it hits end-of-file. The function should print what it reads to the standard output. Reset the stream so that it is valid before returning the stream.

std::istream& func(std::istream &is)
{
    std::string buf;
    while (is >> buf)
        std::cout << buf << std::endl;
    is.clear();
    return is;
}

Exercise 8.2

Exercise 8.3:

What causes the following while to terminate?

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

putting cin in an error state cause to terminate. such as eofbit, failbit and badbit.

Exercise 8.4

Exercise 8.5

Exercise 8.6

Exercise 8.7

Exercise 8.8

Exercise 8.9

Exercise 8.10

Exercise 8.11

Exercise 8.12:

Why didn’t we use in-class initializers in PersonInfo?

Cause we need a aggregate class here. so it should have no in-class initializers.

Exercise 8.13

Exercise 8.14:

Why did we declare entry and nums as const auto &?

  • cause they are all class type, not the built-in type. so reference more effective.
  • output shouldn't change their values. so we added the const.