習題首頁
知識點1:IO類分別定義在三個獨立的頭檔案中
iostream定義了用于讀寫流的基本型別
fstream定義了讀寫命名檔案的型別
sstream定義了讀寫記憶體string物件的型別
習題8.1
istream &iofunc(istream &is) {
string s;
while (is >> s) {
cout << s << endl;
}
is.clear();
return is;
}
習題8.2
#include<iostream>
#include<string>
using namespace std;
istream &iofunc(istream &is) {
string s;
while (is >> s) {
cout << s << endl;
}
is.clear();
return is;
}
int main() {
iofunc(cin);
return 0;
}
知識點2:IO類所定義的一些函式和標志,可以幫助我們訪問和操縱流的條件狀態
badbit用來指出流已經崩潰
failbit用來指出一個IO操作失敗了
eofbit用來指出流達到了檔案結束
習題8.3
badbit、failbit、eofbit三者任意被置位時,檢查流狀態的條件會失敗,//P280
知識點3:頭檔案fstream定義了三個型別來支持檔案IO
ifstream從一個給定檔案讀取資料
ofstream向一個給定檔案寫入資料
fstream可以讀寫給定檔案
習題8.4
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
int fileToVector(string fileName,vector<string> &svec){
ifstream inFile(fileName);
if (!inFile) {
return 1;
}
string s;
/*當 cin 讀取資料時,它會傳遞并忽略任何前導白色空格字符(空格、制表符或換行符),
一旦它接觸到第一個非空格字符即開始閱讀,當它讀取到下一個空白字符時,它將停止讀取,
為了解決這個問題,getline()函式可讀取整行,包括前導和嵌入的空格,并將其存盤在字串物件中,
*/
while (getline(inFile, s)) {
svec.push_back(s);
}
inFile.close();
if (inFile.eof()) {
return 4;
}
if (inFile.bad()) {
return 2;
}
if (inFile.fail()) {
return 3;
}
}
int main() {
vector<string> svec;
string fileName, s;
cout << "Enter fileName:" << endl;
cin >> fileName;
switch (fileToVector(fileName, svec))
{
case 1:
cout << "error: can not open file: " << fileName << endl;
return -1;
case 2:
cout << "error: system failure." << endl;
return -1;
case 3:
cout << "error: read failure." << endl;
return -1;
}
cout << "向量里面的內容:" << endl;
for (vector<string>::iterator iter = svec.begin();iter != svec.end();++iter)
cout << *iter << endl;
return 0;
}
習題8.5
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int fileToVector(string fileName, vector<string>& svec) {
//函式c_str()就是將C++的string轉化為C的字串陣列,c_str()生成一個const char *指標,指向字串的首地址
ifstream inFile(fileName.c_str());
if (!inFile) {
return 1;
}
string s;
//習題8.4一次輸入一行
//while (getline(inFile, s)) {
// svec.push_back(s);
//}
//習題8.5一次一個單詞
while (inFile >> s) {
svec.push_back(s);
}
inFile.close();
if (inFile.eof()) {
return 4;
}
if (inFile.bad()) {
return 2;
}
if (inFile.fail()) {
return 3;
}
}
int main() {
cout << "測驗下" << endl;
vector<string> svec;
string fileName, s;
cout << "Enter filename: ";
cin >> fileName;
switch (fileToVector(fileName,svec))
{
case 1:
cout << "error: can not open file: " << fileName << endl;
return -1;
case 2:
cout << "error: system failure." << endl;
return -1;
case 3:
cout << "error: read failure." << endl;
return -1;
}
cout << "向量里面的內容:" << endl;
for (vector<string>::iterator iter = svec.begin();iter != svec.end();++iter)
cout << *iter << endl;
return 0;
}
習題8.6
P284
知識點4:檔案模式,app每次寫操作均定位到檔案末尾,ate打開檔案后立即定位到檔案末尾,trunc截斷檔案,binary以二進制方式進行IO
·只有當out也被設定時才可以設定trunc模式
·只trunc沒被設定就可以設定app模式
·即使沒有指定trunc,以out模式打開的檔案也會被截斷,trunc是指覆寫存在的檔案 即如果原來檔案中有資料原來的資料就被清空了,
為了保留以out模式打開的檔案的內容,我們必須同時指定app模式,這樣只會將資料追加到檔案末尾,
習題8.7
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Sales_data {
public:
Sales_data() {}
Sales_data(std::string bN, unsigned sold, double reven) :bookNo(bN), units_sold(sold), revenue(reven) {}
std::string isbn() const { return this->bookNo; }
Sales_data& combine(const Sales_data &rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
double avg_price() const {
if (units_sold) {
return revenue / units_sold;
}
else return 0;
}
Sales_data add(const Sales_data &lhs, const Sales_data &rhs) {
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
public:
std::string bookNo; //書號
unsigned units_sold;
double revenue;
};
istream &read(istream &is, Sales_data &item) {
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = item.units_sold * price;
return is;
}
ostream &print(ostream &os, const Sales_data &item) {
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price()<<"\n";
return os;
}
int main(int argc, char **argv)
{
ifstream input(argv[1]);
ofstream output(argv[2]);
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);
cout << endl;
total = trans;
}
}
print(output, total);
cout << endl;
return 0;
}
else
{
cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
}
習題8.8
ofstream output(argv[2],ofstream::app);
知識點5:sstream頭檔案定義了三個型別來支持記憶體IO
istringstream從string讀取資料
ostringstream向string寫入資料
stringstream既可以從string中寫資料,也可以從中讀資料
習題8.9
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
using namespace std;
istream &iofunc(istream &is) {
string s;
while (is >> s) {
cout << s << endl;
}
is.clear();
return is;
}
int main() {
string sss;
cin >> sss;
istringstream iss(sss);
iofunc(iss);
system("pause");
return 0;
}
習題8.10
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int main() {
string infile = "test.txt";
vector<string> svec;
//ifstream定義了一個輸入流in(檔案流),它被初始化從檔案中讀取資料
ifstream in(infile);
if (in){ //養成判斷的習慣
string buf;
while (getline(in, buf)){
svec.push_back(buf);
}
}
else{
cerr << "can not open the file:" << infile << endl;
}
for (auto s : svec){
istringstream iss(s);
string word;
while (iss >> word){
cout << word << endl;
}
}
system("pause");
return 0;
}
習題8.11
istringstream imm;//外部直接定義一個istringstream物件
imm.clear();//回圈內部復位
imm.str(line);//回圈內部將line拷貝到imm中,回傳void
習題8.12
string和vector類內有自己的無參建構式,能夠完成初始化的作業,不會讓其物件處于未定義的狀態,
習題8.13
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<string>
using namespace std;
struct PersonInfo {
string name;
vector<string> phones;
};
int main() {
cout << "Please input the fileName:" << endl;
string infile;
cin >> infile;
ifstream in(infile);
if (!in) {
cerr << "can not open the file: " << infile << endl;
return 0;
}
string line, word;
vector<PersonInfo> people;
istringstream record;
while (getline(in, line)) {
record.str(line);
PersonInfo info;
record >> info.name;
while (record >> word) {
info.phones.push_back(word);
}
record.clear();
people.push_back(info);
}
for (const auto &entry : people) {
cout << entry.name << " ";
for (const auto &ph : entry.phones) {
cout << ph << " ";
}
cout << endl;
}
system("pause");
return 0;
}
習題8.14
將引數定義為&參考:在使用時無需進行賦值,省去很多空間與時間,定義為const:在這個程式中entry和nums并不需要修改,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/13754.html
標籤:C++
