Skip to content

Commit

Permalink
split the ex8_4_5.cpp to ex8_04.cpp and ex8_05.cpp
Browse files Browse the repository at this point in the history
1. use the book.txt of data folder
2. added ex8_06
  • Loading branch information
pezy committed Nov 27, 2014
1 parent 164ba41 commit 6357029
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 84 deletions.
4 changes: 4 additions & 0 deletions ch08/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ 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)
2 changes: 1 addition & 1 deletion ch08/ex8_02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by pezy on 11/27/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// Test your function by calling it, passing cin as an argument
// @Brief Test your function by calling it, passing cin as an argument

#include <iostream>
using std::istream;
Expand Down
37 changes: 37 additions & 0 deletions ch08/ex8_04.cpp
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;
}

37 changes: 37 additions & 0 deletions ch08/ex8_05.cpp
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;
}

47 changes: 47 additions & 0 deletions ch08/ex8_06.cpp
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;
}
83 changes: 0 additions & 83 deletions ch08/ex8_4_5.cpp

This file was deleted.

0 comments on commit 6357029

Please sign in to comment.