《C++ Primer》5th 課後練習 第八章 IO庫 1-10

練習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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章