第八章 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;
}
}