C++ Primer(第5版) 练习 8.13
练习 8.13 重写本节的电话号码程序,从一个命名文件而非cin读取数据。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
/*************************************************************************
> File Name: ex8.13.cpp
> Author:
> Mail:
> Created Time: Sun 25 Feb 2024 07:49:55 PM CST
************************************************************************/
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
struct PersonInfo{
string name;
vector<string> phones;
};
int main(int argc, char *argv[]){
ifstream input(argv[1]);
ofstream output(argv[2], ofstream::app);
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 &p : people){
ostringstream out;
for(const auto &h : p.phones){
out<<" "<<h;
}
output<<p.name<<" "<<out.str()<<endl;
}
return 0;
}