练习8.1 编写函数,接受一个istream&
参数,返回值类型也是istream&
。此函数须从给定流中读取数据,直至遇到文件结束标识时停止。它将读取的数据打印在标准输出上。完成这些操作后,在返回流之前,对流进行复位,使其处于有效状态。
istream &func(istream &in) {
char c;
while (in >> c) {
cout << c;
}
in.clear();
return in;
}
练习8.2 测试函数,调用参数为cin
。
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
istream &func(istream &in) {
char c;
while (in >> c) {
cout << c;
}
in.clear();
return in;
}
int main(int argc, char **argv)
{
istream &testin = func(cin);
cout << testin.rdstate() << endl;
return 0;
}
练习8.3 什么情况下,下面的while循环会终止?
while (cin >> i) /* ... */
当发生io
错误时,比如i
是一个int
型变量却输入一个char
型数据。(failbit
被置位)
当输入eof
结束符时。(eofbit
被置位,eof
被置位)
当该输入流崩溃时。(badbit
被置位,failbit
被置位)
练习8.4 编写函数,以读模式打开一个文件,将其内容读入到一个string的vector中,将每一行作为一个独立的元素存于vector中。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
int main(int argc, char **argv)
{
vector<string> vec;
ifstream fin("test.txt");
string s;
if(fin){
while (getline(fin, s)) {
vec.push_back(s);
}
}
for (auto &item : vec) {
cout << item << endl;
}
return 0;
}
练习8.5 重写上面的程序,将每个单词作为一个独立的元素进行存储。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
int main(int argc, char **argv)
{
vector<string> vec;
ifstream fin("test.txt");
string s;
if (fin) {
while (fin >> s) {
vec.push_back(s);
}
}
for (auto &item : vec) {
cout << item << endl;
}
return 0;
}
练习8.6 重写7.1.1节的书店程序,从一个文件中读取交易记录。将文件名作为一个参数传递给main。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "Sale_data.h"
using namespace std;
int main(int argc, char **argv)
{
ifstream fin(argv[1]);
Sales_data total;
if (!fin) {
cerr << "open error" << endl;
return 0;
}
if (read(fin, total)) {
Sales_data trans;
while (read(fin, trans)) {
if (total.isbn() == trans.isbn())
total.combine(trans);
else {
print(cout, total) << endl;
total = trans;
}
}
print(cout, total) << endl;
}
else {
cerr << "No data?!" << endl;
}
return 0;
}
练习8.7 修改上一节的书店程序,将结果保存到一个文件中。将输出文件名作为第二个参数传递给main函数。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "Sale_data.h"
using namespace std;
int main(int argc, char **argv)
{
ifstream fin(argv[1]);
ofstream fout(argv[2]);
Sales_data total;
if (!fin) {
cerr << "open error" << endl;
return 0;
}
if (read(fin, total)) {
Sales_data trans;
while (read(fin, trans)) {
if (total.isbn() == trans.isbn())
total.combine(trans);
else {
print(fout, total) << endl;
total = trans;
}
}
print(fout, total) << endl;
}
else {
cerr << "No data?!" << endl;
}
return 0;
}
练习8.8 修改上一题的程序,将结果追加到给定的文件末尾。对同一个输出文件,运行程序至少两次,检验数据是否得以保留。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "Sale_data.h"
using namespace std;
int main(int argc, char **argv)
{
ifstream fin(argv[1]);
ofstream fout(argv[2], ofstream::out | ofstream::app);
Sales_data total;
if (!fin) {
cerr << "open error" << endl;
return 0;
}
if (read(fin, total)) {
Sales_data trans;
while (read(fin, trans)) {
if (total.isbn() == trans.isbn())
total.combine(trans);
else {
print(fout, total) << endl;
total = trans;
}
}
print(fout, total) << endl;
}
else {
cerr << "No data?!" << endl;
}
return 0;
}
练习8.9 使用你为8.1.2节第一个练习所编写的函数打印一个istringstream
对象的内容。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "Sale_data.h"
using namespace std;
istream &func(istream &in) {
char c;
while (in >> c) {
cout << c;
}
in.clear();
return in;
}
int main(int argc, char **argv)
{
string s("aaa bbb\n ccc");
istringstream sin(s);
func(sin);
return 0;
}
练习8.10 编写程序,将来自一个文件中的行保存在一个vector中。然后使用一个istringstream
从vector读取数据元素,每次读取一个单词。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main(int argc, char **argv)
{
string instr, outstr;
vector<string> vec;
string file = "./123.txt";
/*这个地方很有意思,我用的IDE是VS2017,
*用绝对路径就可以找到文件,而相对路径则不行
*原因是,IDE在运行.exe文件时改变了运行目录
*想用相对路径寻址的话得更改IDE设置,或者去Debug目录下直接双击.exe文件
*/
ifstream fin(file);
//cout << argv[1] << endl;
//cout << fin.good() << endl;
//cout << cin.good() << endl;
if (fin) {
while (fin >> instr) {
vec.push_back(instr);
}
for (auto &ss : vec) {
istringstream sin(ss);
sin >> outstr;
cout << outstr << endl;
}
}
system("pause");
return 0;
}