C++ Primer(第五版)课后习题记录 —— 第八章
2018-04-12 16:12
441 查看
第八章 IO库
练习8.1
istream& input(istream& in) { std::string buff; while(in >> buff) std::cout << buff; in.clear(); return in; }
练习8.2
int main() { istream& in = input(cin); cout << "----------" <<endl; cout << in.good() << endl; return 0; }
练习8.3
badbit、failbit 和 eofbit 中的任意一个被置位,检测流状态的条件就会失败。练习8.4
#include <iostream> #include <string> #include <fstream> #include <vector> using namespace std; int main() { vector<string> ivec; ifstream in("8_4"); if(in) { string put; while (in) { getline(in, put); ivec.push_back(put); } } for (auto c : ivec) cout << c <<endl; return 0; }
练习8.5
int main() { vector<string> ivec; ifstream in("8_4"); if(in) { string put; while (in) { in >> put; //只改了这句就行 ivec.push_back(put); } } for (auto c : ivec) cout << c <<endl; return 0; }
练习8.6
#include <iostream> #include <string> #include <fstream> #include "Sales_data.h" using namespace std; int main(int argc, char** argv) { ifstream input(argv[1]); Sales_data total; if (read(input, total)) { Sales_data trans; while (read(input, trans)) { if (total.isbn() == trans.isbn()) total.combine(trans); else { print(cout, total) << endl; total = trans; } } print(cout, total) << endl; } else { cerr << "No data?!" << std::endl; return -1; } return 0; }
练习8.7
#include <iostream> #include <fstream> #include "Sales_data.h" using namespace std; int main(int argc, char** argv) { ifstream input(argv[1]); ofstream output(argv[2], ofstream::out); Sales_data total; if (read(input, total)) { Sales_data trans; while (read(input, trans)) { if (total.isbn() == trans.isbn()) total.combine(trans); else { print(output, total) << endl; total = trans; } } print(output, total) << endl; } else { cerr << "No data?!" << std::endl; return -1; } return 0; }
练习8.8
将 output 打开的文件模式设定如下ofstream output(argv[2], ofstream::out | ofstream::app);
即可。
练习8.9
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
istream& input(istream& in) { std::string buff; while(in >> buff) std::cout << buff; in.clear(); return in; }
int main()
{
istringstream ss("hola~11");
input(ss);
return 0;
}
练习8.10
#include <iostream> #include <fstream> #include <sstream> #include <vector> #include <string> using namespace std; int main() { fstream input("8_4"); vector<string> strs; string buf; while(getline(input, buf)) strs.push_back(buf); for (auto c : strs) { istringstream buff(c); string bufff; while (buff >> bufff) cout << bufff << endl; } }
练习8.11
//只录入改变部分 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); record.clear(); //重点是要重置流 }
练习8.12
如果类内初始化,最后生成的对象中就会有无意义的值。练习8.13
#include <iostream> #include <fstream> #include <sstream> #include <vector> #include <string> using namespace std; struct PersonInfo { string name; vector<string> phones; }; bool valid(string s) { return isdigit(s[0]); } string format(const string& str) { return "+" + str.substr(0,2) + " " + str.substr(2,3) + "-" + str.substr(5) ; } int main() { fstream input("sstream"); string line, word; vector<PersonInfo> people; while(getline(input, 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; } }
相关文章推荐
- C++ Primer(第五版)课后习题记录 —— 第三章
- C++ Primer(第五版)课后习题记录 —— 第七章
- C++ Primer(第五版)课后习题记录 —— 第五章
- C++ Primer(第五版)课后习题记录 —— 第四章
- 《C++ Primer》第五版课后习题解答_第二章(1)(01-08)
- 《C++ Primer》第五版课后习题解答_第三章(5)(37-45)
- 《C++ Primer》第五版课后习题解答_第六章(4)(27-35)
- 《C++ Primer》第五版课后习题解答_第四章(1)(01-20)
- 《C++ Primer》第五版课后习题解答_第五章(3)(15-19)
- 《C++ Primer》第五版课后习题解答_第四章(3)(34-38)
- 《C++ Primer》第五版课后习题解答_第三章(4)(30-36)
- c++ primer(第五版)学习笔记及习题答案代码版(第八章)IO库
- 《C++ Primer》第五版课后习题解答_第二章(2)(09-20)
- 《C++ Primer》第五版课后习题解答_第三章(1)(01-05)
- 《C++ Primer》第五版课后习题解答_第二章(3)(21-29)
- 《C++ Primer》第五版课后习题解答_第五章(2)(09-14)
- 《C++ Primer》第五版课后习题解答_第二章(4)(30-38)
- 《C++ Primer》第五版课后习题解答_第一章(1)(01-11)
- C++ Primer 课后习题9.39 统计单词个数并记录最大单词和最短单词
- 《C++ Primer》第五版课后习题解答_第五章(1)(01-08)