forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request pezy#80 from pezy/master
Finished Chapter 8.
- Loading branch information
Showing
15 changed files
with
467 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Chapter 8. The IO Library | ||
|
||
## Exercise 8.1: | ||
>Write a function that takes and returns an istream&. The function should read the stream until it hits end-of-file. The function should print what it reads to the standard output. Reset the stream so that it is valid before returning the stream. | ||
```cpp | ||
istream& func(istream &is) | ||
{ | ||
std::string buf; | ||
while (is >> buf) | ||
std::cout << buf << std::endl; | ||
is.clear(); | ||
return is; | ||
} | ||
``` | ||
## [Exercise 8.2](ex8_02.cpp) | ||
## Exercise 8.3: | ||
>What causes the following while to terminate? | ||
```cpp | ||
while (cin >> i) /* ... */ | ||
``` | ||
|
||
putting `cin` in an error state cause to terminate. such as `eofbit`, `failbit` and `badbit`. | ||
|
||
## [Exercise 8.4](ex8_04.cpp) | ||
## [Exercise 8.5](ex8_05.cpp) | ||
## [Exercise 8.6](ex8_06.cpp) | ||
## [Exercise 8.7](ex8_07.cpp) | ||
## [Exercise 8.8](ex8_08.cpp) | ||
## [Exercise 8.9](ex8_09.cpp) | ||
## [Exercise 8.10](ex8_10.cpp) | ||
## [Exercise 8.11](ex8_11.cpp) | ||
|
||
## Exercise 8.12: | ||
>Why didn’t we use in-class initializers in PersonInfo? | ||
Cause we need a aggregate class here. so it should have no in-class initializers. | ||
|
||
## [Exercise 8.13](ex8_13.cpp) | ||
|
||
## Exercise 8.14: | ||
>Why did we declare entry and nums as const auto &? | ||
- cause they are all class type, not the built-in type. so **reference** more effective. | ||
- output shouldn't change their values. so we added the `const`. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// ex8_02.cpp | ||
// Exercise 8.2 | ||
// | ||
// Created by pezy on 11/27/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @Brief Test your function by calling it, passing cin as an argument | ||
|
||
#include <iostream> | ||
using std::istream; | ||
|
||
istream& func(istream &is) | ||
{ | ||
std::string buf; | ||
while (is >> buf) | ||
std::cout << buf << std::endl; | ||
is.clear(); | ||
return is; | ||
} | ||
|
||
int main() | ||
{ | ||
istream& is = func(std::cin); | ||
std::cout << is.rdstate() << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// ex8_04.cpp | ||
// Exercise 8.4 | ||
// | ||
// Created by pezy on 11/9/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @Brief Write a function to open a file for input and read its contents into a vector of strings, | ||
// storing each line as a separate element in the vector. | ||
|
||
#include <fstream> | ||
#include <string> | ||
#include <vector> | ||
#include <iostream> | ||
|
||
using std::vector; using std::string; using std::ifstream; using std::cout; using std::endl; | ||
|
||
void ReadFileToVec(const string& fileName, vector<string>& vec) | ||
{ | ||
ifstream ifs(fileName); | ||
if (ifs) | ||
{ | ||
string buf; | ||
while (std::getline(ifs, buf)) | ||
vec.push_back(buf); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
vector<string> vec; | ||
ReadFileToVec("../data/book.txt", vec); | ||
for (const auto &str : vec) | ||
cout << str << endl; | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// ex8_05.cpp | ||
// Exercise 8.5 | ||
// | ||
// Created by pezy on 11/9/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @Brief Rewrite the previous program to store each word in a separate element. | ||
// @See ex8_04.cpp | ||
|
||
#include <fstream> | ||
#include <string> | ||
#include <vector> | ||
#include <iostream> | ||
|
||
using std::vector; using std::string; using std::ifstream; using std::cout; using std::endl; | ||
|
||
void ReadFileToVec(const string& fileName, vector<string>& vec) | ||
{ | ||
ifstream ifs(fileName); | ||
if (ifs) | ||
{ | ||
string buf; | ||
while (ifs >> buf) | ||
vec.push_back(buf); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
vector<string> vec; | ||
ReadFileToVec("../data/book.txt", vec); | ||
for (const auto &str : vec) | ||
cout << str << endl; | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// ex8_06.cpp | ||
// Exercise 8.6 | ||
// | ||
// Created by pezy on 11/27/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @Brief Rewrite the bookstore program from ¡ì7.1.1 (p.256) to read its transactions from a file. | ||
// Pass the name of the file as an argument to main (¡ì6.2.5, p.218). | ||
// @See ex7_26.h (use the Sales_data) | ||
// @Run give a parameter: "../data/book.txt" | ||
// @Output 0-201-78345-X 5 110 | ||
// 0-201-78346-X 9 839.2 | ||
|
||
#include <fstream> | ||
#include <iostream> | ||
|
||
#include "../ch07/ex7_26.h" | ||
using std::ifstream; using std::cout; using std::endl; using std::cerr; | ||
|
||
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?!" << endl; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// ex8_07.cpp | ||
// Exercise 8.7 | ||
// | ||
// Created by pezy on 11/28/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @Brief Revise the bookstore program from the previous section to write its output to a file. | ||
// Pass the name of that file as a second argument to main. | ||
// @See ex8_06.cpp | ||
// @Run give a parameter: "../data/book.txt ../data/out.txt" | ||
|
||
#include <fstream> | ||
#include <iostream> | ||
|
||
#include "../ch07/ex7_26.h" | ||
using std::ifstream; using std::ofstream; using std::endl; using std::cerr; | ||
|
||
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) << endl; | ||
total = trans; | ||
} | ||
} | ||
print(output, total) << endl; | ||
} | ||
else | ||
{ | ||
cerr << "No data?!" << endl; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.