zoukankan      html  css  js  c++  java
  • 第八章 IO库

    8.1&&8.2

     1 #include <iostream>
     2 #include <vector> 
     3 #include <string>
     4  
     5 using namespace std;
     6 
     7 istream &func(istream &is)
     8 {
     9     int val;
    10     while (is >> val && !is.eof()) {
    11         cout << val << endl;
    12     }
    13     is.clear();        //复位所有错误标志位 
    14 }
    15 
    16 int main()
    17 {
    18     func(cin);
    19     return 0;
    20 }
    View Code

    8.3

    读取类型不匹配;遇到文件结束符EOF;IO流错误

    8.4

     1 #include <iostream>
     2 #include <fstream> 
     3 #include <vector> 
     4 #include <string>
     5  
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     vector<string> vec;
    11     string ifile = "data.txt", ss;    //需在本文件夹下有data.txt这个文件 
    12     ifstream in(ifile);
    13     while(getline(in, ss)) {
    14         vec.push_back(ss);
    15     }
    16     in.close();
    17     for (auto i : vec)
    18         cout << i << endl;
    19     return 0;
    20 }
    View Code

    8.5

     1 #include <iostream>
     2 #include <fstream> 
     3 #include <vector> 
     4 #include <string>
     5  
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     vector<string> vec;
    11     string ifile = "data.txt", ss;    //需在本文件夹下有data.txt这个文件 
    12     ifstream in(ifile);
    13     while(in >> ss) {            //改动处 
    14         vec.push_back(ss);
    15     }
    16     in.close();
    17     for (auto i : vec)
    18         cout << i << endl;
    19     return 0;
    20 }
    View Code

    8.6

     1 #include <iostream>
     2 #include <fstream> 
     3 #include <vector> 
     4 #include <string>
     5  
     6 using namespace std;
     7 
     8 struct Sales_data {
     9     string bookNo;                //书的ISBN 
    10     unsigned units_sold = 0;    //售出的本数 
    11     double revenue = 0.0;        //销售额 
    12     Sales_data& combine(const Sales_data &);
    13     string isbn()    {    return bookNo;    }
    14 };
    15 
    16 Sales_data &Sales_data::combine(const Sales_data &rhs)
    17 {
    18     units_sold += rhs.units_sold;
    19     revenue += rhs.revenue;
    20     return *this;
    21 }
    22 
    23 istream &read(istream &is, Sales_data &item)
    24 {
    25     is >> item.bookNo >> item.units_sold >> item.revenue;
    26     return is;
    27 }
    28 
    29 ostream &print(ostream &os, const Sales_data &item)
    30 {
    31     os << item.bookNo << " " << item.units_sold << " " << item.revenue;
    32     return os;
    33 }
    34 
    35 Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
    36 {
    37     Sales_data sum = lhs;
    38     sum.combine(rhs);
    39     return sum;
    40 }
    41 
    42 int main(int argc, char *argv[])
    43 {
    44     if (argc != 2) {  
    45         cerr<< "Please give the file name."<<endl;  
    46         return -1;  
    47     }
    48     ifstream in(argv[1]);
    49     if (!in) {  
    50         cerr<<"Can't open the file."<<endl;  
    51         return -1;  
    52     }
    53     Sales_data total;
    54     if (read(in, total)) {
    55         Sales_data trans;
    56         while (read(in, trans)) {
    57             if (total.isbn() == trans.isbn())    total = add(total, trans);
    58             else {
    59                 print(cout, total);
    60                 cout << endl;
    61                 total = trans;        //结构体赋值很方便 
    62             }
    63         }
    64         cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
    65     }
    66     else {
    67         cerr << "No data?!" << endl;
    68         return -1;
    69     }
    70     return 0;
    71 }
    View Code

     文件名(book.txt)通过参数传给main函数:

    8.7

     1 #include <iostream>
     2 #include <fstream> 
     3 #include <vector> 
     4 #include <string>
     5  
     6 using namespace std;
     7 
     8 struct Sales_data {
     9     string bookNo;                //书的ISBN 
    10     unsigned units_sold = 0;    //售出的本数 
    11     double revenue = 0.0;        //销售额 
    12     Sales_data& combine(const Sales_data &);
    13     string isbn()    {    return bookNo;    }
    14 };
    15 
    16 Sales_data &Sales_data::combine(const Sales_data &rhs)
    17 {
    18     units_sold += rhs.units_sold;
    19     revenue += rhs.revenue;
    20     return *this;
    21 }
    22 
    23 istream &read(istream &is, Sales_data &item)
    24 {
    25     is >> item.bookNo >> item.units_sold >> item.revenue;
    26     return is;
    27 }
    28 
    29 ostream &print(ostream &os, const Sales_data &item)
    30 {
    31     os << item.bookNo << " " << item.units_sold << " " << item.revenue;
    32     return os;
    33 }
    34 
    35 Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
    36 {
    37     Sales_data sum = lhs;
    38     sum.combine(rhs);
    39     return sum;
    40 }
    41 
    42 int main(int argc, char *argv[])
    43 {
    44     if (argc != 3) {  
    45         cerr<< "Please give the input file name and output file name."<<endl;  
    46         return -1;  
    47     }
    48     ifstream in(argv[1]);
    49     ofstream out(argv[2]);
    50     if (!in) {  
    51         cerr<<"Can't open the file."<<endl;  
    52         return -1;  
    53     }
    54     if (!out) {  
    55         cerr<<"can't open output file."<<endl;  
    56         return -1;  
    57     } 
    58     Sales_data total;
    59     if (read(in, total)) {
    60         Sales_data trans;
    61         while (read(in, trans)) {
    62             if (total.isbn() == trans.isbn())    total = add(total, trans);
    63             else {
    64                 print(out, total) << endl;        //输出到指定文件中 
    65                 total = trans;
    66             }
    67         }
    68         print(out, total) << endl;
    69     }
    70     else {
    71         cerr << "No data?!" << endl;
    72         return -1;
    73     }
    74     return 0;
    75 }
    View Code

    将上一题输出的内容输出到文件book1.txt中:

    8.8

     1 #include <iostream>
     2 #include <fstream> 
     3 #include <vector> 
     4 #include <string>
     5  
     6 using namespace std;
     7 
     8 struct Sales_data {
     9     string bookNo;                //书的ISBN 
    10     unsigned units_sold = 0;    //售出的本数 
    11     double revenue = 0.0;        //销售额 
    12     Sales_data& combine(const Sales_data &);
    13     string isbn()    {    return bookNo;    }
    14 };
    15 
    16 Sales_data &Sales_data::combine(const Sales_data &rhs)
    17 {
    18     units_sold += rhs.units_sold;
    19     revenue += rhs.revenue;
    20     return *this;
    21 }
    22 
    23 istream &read(istream &is, Sales_data &item)
    24 {
    25     is >> item.bookNo >> item.units_sold >> item.revenue;
    26     return is;
    27 }
    28 
    29 ostream &print(ostream &os, const Sales_data &item)
    30 {
    31     os << item.bookNo << " " << item.units_sold << " " << item.revenue;
    32     return os;
    33 }
    34 
    35 Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
    36 {
    37     Sales_data sum = lhs;
    38     sum.combine(rhs);
    39     return sum;
    40 }
    41 
    42 int main(int argc, char *argv[])
    43 {
    44     if (argc != 3) {  
    45         cerr<< "Please give the input file name and output file name."<<endl;  
    46         return -1;  
    47     }
    48     ifstream in(argv[1]);
    49     ofstream out(argv[2], ofstream::app);        //唯一改变之处 
    50     if (!in) {  
    51         cerr<<"Can't open the file."<<endl;  
    52         return -1;  
    53     }
    54     if (!out) {  
    55         cerr<<"can't open output file."<<endl;  
    56         return -1;  
    57     } 
    58     Sales_data total;
    59     if (read(in, total)) {
    60         Sales_data trans;
    61         while (read(in, trans)) {
    62             if (total.isbn() == trans.isbn())    total = add(total, trans);
    63             else {
    64                 print(out, total) << endl;
    65                 total = trans;
    66             }
    67         }
    68         print(out, total) << endl;
    69     }
    70     else {
    71         cerr << "No data?!" << endl;
    72         return -1;
    73     }
    74     return 0;
    75 }
    View Code

    8.9

     1 #include <iostream>
     2 #include <vector> 
     3 #include <string>
     4 #include <sstream> 
     5  
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     string line, word;    
    11     while(getline(cin, line)) { 
    12         istringstream in(line);
    13         while (in >> word) 
    14             cout << word << endl;
    15     }
    16     return 0;
    17 }
    View Code

    8.10

     1 #include <iostream>
     2 #include <vector> 
     3 #include <string>
     4 #include <fstream> 
     5 #include <sstream> 
     6  
     7 using namespace std;
     8 
     9 int main()
    10 {
    11     vector<string> vec;
    12     string line, word;
    13     ifstream input("data.txt");
    14     while(getline(input, line)) { 
    15         vec.push_back(line);
    16     }
    17     for (auto i : vec) {
    18         istringstream in(i);
    19         while (in >> word) {
    20             cout << word << endl;
    21         }    
    22     }
    23     return 0;
    24 }
    View Code

    8.11

     1 #include <iostream>
     2 #include <vector> 
     3 #include <string>
     4 #include <fstream> 
     5 #include <sstream> 
     6  
     7 using namespace std;
     8 
     9 struct PersonInfo {  
    10     string name;  
    11     vector<string> phones;  
    12 };  
    13   
    14 int main()  
    15 {  
    16     string line, word;  
    17     vector<PersonInfo> people;  
    18     istringstream record;      //定义在循环之外 
    19     while (getline(cin,line)) {   
    20         PersonInfo info;  
    21         record.clear();      //每次要解除上次绑定的string对象 
    22         record.str(line);  
    23         record >> info.name;  
    24         while (record >> word)  
    25             info.phones.push_back(word);  
    26         people.push_back(info);  
    27     }  
    28     return 0;  
    29 }
    View Code 

    8.12

    因为此时我们需要其中的成员皆可被访问,所以 PersonInfo 是一个聚合类,不能在内部进行初始化

    8.13

     1 #include <iostream>
     2 #include <vector> 
     3 #include <string>
     4 #include <fstream> 
     5 #include <sstream> 
     6  
     7 using namespace std;
     8 
     9 struct PersonInfo {  
    10     string name;  
    11     vector<string> phones;  
    12 };  
    13   
    14 int main()  
    15 {  
    16     ifstream in("data.txt");         //从文件读取 
    17     string line, word;  
    18     vector<PersonInfo> people;  
    19     istringstream record;      
    20     while (getline(in,line)) {      //in为文件输入流 
    21         PersonInfo info;  
    22         record.clear();  
    23         record.str(line);  
    24         record >> info.name;  
    25         while (record >> word)  
    26             info.phones.push_back(word);  
    27         people.push_back(info);  
    28     }  
    29     return 0;  
    30 }  
    View Code

    8.14

    这两条语句分别适用范围for语句枚举people中所有项和每项的phones中的所有项。

    使用const表明在循环中不会改变这些项的值;

    使用auto是请求编译器依据vector元素类型来推断出entry和nums的类型,既简化代码又避免出错;

    使用引用的原因是,people和phones的元素分别是结构对象和字符串对象,使用引用即可避免对象拷贝。

  • 相关阅读:
    获取css信息
    html嵌套规则
    js获取ip地址
    match excel test search replace 用法
    js 宽和高
    判断类型 从零开始系列
    js随机数 从头开始系列
    苹果自带拼音转换方法
    iOS GCD 拾遗
    iOS用户响应者链的那些事儿
  • 原文地址:https://www.cnblogs.com/xzxl/p/7681844.html
Copyright © 2011-2022 走看看