Skip to content

Commit

Permalink
added ex8.7~8.8
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Nov 28, 2014
1 parent 6357029 commit 68700c8
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ch08/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ putting `cin` in an error state cause to terminate. such as `eofbit`, `failbit`
## [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)
46 changes: 46 additions & 0 deletions ch08/ex8_07.cpp
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;
}
46 changes: 46 additions & 0 deletions ch08/ex8_08.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// ex8_08.cpp
// Exercise 8.8
//
// Created by pezy on 11/28/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief Revise the program from the previous exercise to append its output to its given file.
// Run the program on the same output file at least twice to ensure that the data are preserved
// @See ex8_07.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], ofstream::app);

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;
}

0 comments on commit 68700c8

Please sign in to comment.