forked from ShujiaHuang/Cpp-Primer-Plus-6th
-
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.
- Loading branch information
1 parent
b626d5b
commit d0712cc
Showing
25 changed files
with
1,216 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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,56 @@ | ||
// append.cpp -- appending information to a file | ||
#include <iostream> | ||
#include <fstream> | ||
#include <string> | ||
#include <cstdlib> // (or stdlib.h) for exit() | ||
|
||
const char * file = "guests.txt"; | ||
int main() | ||
{ | ||
using namespace std; | ||
char ch; | ||
|
||
// show initial contents | ||
ifstream fin; | ||
fin.open(file); | ||
|
||
if (fin.is_open()) | ||
{ | ||
cout << "Here are the current contents of the " | ||
<< file << " file:\n"; | ||
while (fin.get(ch)) | ||
cout << ch; | ||
fin.close(); | ||
} | ||
|
||
// add new names | ||
ofstream fout(file, ios::out | ios::app); | ||
if (!fout.is_open()) | ||
{ | ||
cerr << "Can't open " << file << " file for output.\n"; | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
cout << "Enter guest names (enter a blank line to quit):\n"; | ||
string name; | ||
while (getline(cin,name) && name.size() > 0) | ||
{ | ||
fout << name << endl; | ||
} | ||
fout.close(); | ||
|
||
// show revised file | ||
fin.clear(); // not necessary for some compilers | ||
fin.open(file); | ||
if (fin.is_open()) | ||
{ | ||
cout << "Here are the new contents of the " | ||
<< file << " file:\n"; | ||
while (fin.get(ch)) | ||
cout << ch; | ||
fin.close(); | ||
} | ||
cout << "Done.\n"; | ||
// cin.get(); | ||
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,88 @@ | ||
// binary.cpp -- binary file I/O | ||
#include <iostream> // not required by most systems | ||
#include <fstream> | ||
#include <iomanip> | ||
#include <cstdlib> // (or stdlib.h) for exit() | ||
|
||
inline void eatline() { while (std::cin.get() != '\n') continue; } | ||
struct planet | ||
{ | ||
char name[20]; // name of planet | ||
double population; // its population | ||
double g; // its acceleration of gravity | ||
}; | ||
|
||
const char * file = "planets.dat"; | ||
|
||
int main() | ||
{ | ||
using namespace std; | ||
planet pl; | ||
cout << fixed << right; | ||
|
||
// show initial contents | ||
ifstream fin; | ||
fin.open(file, ios_base::in |ios_base::binary); // binary file | ||
//NOTE: some systems don't accept the ios_base::binary mode | ||
if (fin.is_open()) | ||
{ | ||
cout << "Here are the current contents of the " | ||
<< file << " file:\n"; | ||
while (fin.read((char *) &pl, sizeof pl)) | ||
{ | ||
cout << setw(20) << pl.name << ": " | ||
<< setprecision(0) << setw(12) << pl.population | ||
<< setprecision(2) << setw(6) << pl.g << endl; | ||
} | ||
fin.close(); | ||
} | ||
|
||
// add new data | ||
ofstream fout(file, | ||
ios_base::out | ios_base::app | ios_base::binary); | ||
//NOTE: some systems don't accept the ios::binary mode | ||
if (!fout.is_open()) | ||
{ | ||
cerr << "Can't open " << file << " file for output:\n"; | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
cout << "Enter planet name (enter a blank line to quit):\n"; | ||
cin.get(pl.name, 20); | ||
while (pl.name[0] != '\0') | ||
{ | ||
eatline(); | ||
cout << "Enter planetary population: "; | ||
cin >> pl.population; | ||
cout << "Enter planet's acceleration of gravity: "; | ||
cin >> pl.g; | ||
eatline(); | ||
fout.write((char *) &pl, sizeof pl); | ||
cout << "Enter planet name (enter a blank line " | ||
"to quit):\n"; | ||
cin.get(pl.name, 20); | ||
} | ||
fout.close(); | ||
|
||
// show revised file | ||
fin.clear(); // not required for some implementations, but won't hurt | ||
fin.open(file, ios_base::in | ios_base::binary); | ||
if (fin.is_open()) | ||
{ | ||
cout << "Here are the new contents of the " | ||
<< file << " file:\n"; | ||
while (fin.read((char *) &pl, sizeof pl)) | ||
{ | ||
cout << setw(20) << pl.name << ": " | ||
<< setprecision(0) << setw(12) << pl.population | ||
<< setprecision(2) << setw(6) << pl.g << endl; | ||
} | ||
fin.close(); | ||
} | ||
cout << "Done.\n"; | ||
// keeping output window open | ||
// cin.clear(); | ||
// eatline(); | ||
// cin.get(); | ||
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,26 @@ | ||
// check_it.cpp -- checking for valid input | ||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
using namespace std; | ||
cout << "Enter numbers: "; | ||
|
||
int sum = 0; | ||
int input; | ||
while (cin >> input) | ||
{ | ||
sum += input; | ||
} | ||
|
||
cout << "Last value entered = " << input << endl; | ||
cout << "Sum = " << sum << endl; | ||
/* keeping output window open */ | ||
/* | ||
cin.clear(); | ||
while (cin.get() != '\n') | ||
continue; | ||
cin.get(); | ||
*/ | ||
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,35 @@ | ||
// cinexcp.cpp -- having cin throw an exception | ||
#include <iostream> | ||
#include <exception> | ||
|
||
int main() | ||
{ | ||
using namespace std; | ||
// have failbit cause an exception to be thrown | ||
cin.exceptions(ios_base::failbit); | ||
cout << "Enter numbers: "; | ||
int sum = 0; | ||
int input; | ||
try { | ||
while (cin >> input) | ||
{ | ||
sum += input; | ||
} | ||
} catch(ios_base::failure & bf) | ||
{ | ||
cout << bf.what() << endl; | ||
cout << "O! the horror!\n"; | ||
} | ||
|
||
cout << "Last value entered = " << input << endl; | ||
cout << "Sum = " << sum << endl; | ||
/* keeping output window open */ | ||
/* | ||
cin.clear(); | ||
while (cin.get() != '\n') | ||
continue; | ||
cin.get(); | ||
*/ | ||
|
||
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,39 @@ | ||
// count.cpp -- counting characters in a list of files | ||
#include <iostream> | ||
#include <fstream> | ||
#include <cstdlib> // or stdlib.h | ||
int main(int argc, char * argv[]) | ||
{ | ||
using namespace std; | ||
if (argc == 1) // quit if no arguments | ||
{ | ||
cerr << "Usage: " << argv[0] << " filename[s]\n"; | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
ifstream fin; // open stream | ||
long count; | ||
long total = 0; | ||
char ch; | ||
|
||
for (int file = 1; file < argc; file++) | ||
{ | ||
fin.open(argv[file]); // connect stream to argv[file] | ||
if (!fin.is_open()) | ||
{ | ||
cerr << "Could not open " << argv[file] << endl; | ||
fin.clear(); | ||
continue; | ||
} | ||
count = 0; | ||
while (fin.get(ch)) | ||
count++; | ||
cout << count << " characters in " << argv[file] << endl; | ||
total += count; | ||
fin.clear(); // needed for some implementations | ||
fin.close(); // disconnect file | ||
} | ||
cout << total << " characters in all files\n"; | ||
|
||
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,30 @@ | ||
// defaults.cpp -- cout default formats | ||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
using std::cout; | ||
cout << "12345678901234567890\n"; | ||
char ch = 'K'; | ||
int t = 273; | ||
cout << ch << ":\n"; | ||
cout << t << ":\n"; | ||
cout << -t <<":\n"; | ||
|
||
double f1 = 1.200; | ||
cout << f1 << ":\n"; | ||
cout << (f1 + 1.0 / 9.0) << ":\n"; | ||
|
||
double f2 = 1.67E2; | ||
cout << f2 << ":\n"; | ||
f2 += 1.0 / 9.0; | ||
cout << f2 << ":\n"; | ||
cout << (f2 * 1.0e4) << ":\n"; | ||
|
||
|
||
double f3 = 2.3e-4; | ||
cout << f3 << ":\n"; | ||
cout << f3 / 10 << ":\n"; | ||
// std::cin.get(); | ||
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,35 @@ | ||
// fileio.cpp -- saving to a file | ||
#include <iostream> // not needed for many systems | ||
#include <fstream> | ||
#include <string> | ||
|
||
int main() | ||
{ | ||
using namespace std; | ||
string filename; | ||
|
||
cout << "Enter name for new file: "; | ||
cin >> filename; | ||
|
||
// create output stream object for new file and call it fout | ||
ofstream fout(filename.c_str()); | ||
|
||
fout << "For your eyes only!\n"; // write to file | ||
cout << "Enter your secret number: "; // write to screen | ||
float secret; | ||
cin >> secret; | ||
fout << "Your secret number is " << secret << endl; | ||
fout.close(); // close file | ||
|
||
// create input stream object for new file and call it fin | ||
ifstream fin(filename.c_str()); | ||
cout << "Here are the contents of " << filename << ":\n"; | ||
char ch; | ||
while (fin.get(ch)) // read character from file and | ||
cout << ch; // write it to screen | ||
cout << "Done\n"; | ||
fin.close(); | ||
// std::cin.get(); | ||
// std::cin.get(); | ||
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,32 @@ | ||
// files.cpp -- saving to a file | ||
#include <iostream> // not needed for many systems | ||
#include <fstream> | ||
#include <string> | ||
#include <sstream> | ||
|
||
int main() | ||
{ | ||
using namespace std; | ||
string filename = "file"; | ||
|
||
|
||
|
||
// create output stream object for new file and call it fout | ||
int i; | ||
for (i = 0; i < 140; i++) | ||
{ | ||
ostringstream outstr; // manages a string stream | ||
outstr << filename << i; | ||
string fname = outstr.str(); | ||
ofstream fout(fname.c_str()); | ||
if (!fout.is_open()) | ||
break; | ||
fout << "For your eyes only!\n"; // write to file | ||
fout.close(); // close file | ||
fout.clear(); | ||
} | ||
cout << "i: " << i << endl; | ||
// std::cin.get(); | ||
// std::cin.get(); | ||
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,19 @@ | ||
// fill.cpp -- changing fill character for fields | ||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
using std::cout; | ||
cout.fill('*'); | ||
const char * staff[2] = { "Waldo Whipsnade", "Wilmarie Wooper"}; | ||
long bonus[2] = {900, 1350}; | ||
|
||
for (int i = 0; i < 2; i++) | ||
{ | ||
cout << staff[i] << ": $"; | ||
cout.width(7); | ||
cout << bonus[i] << "\n"; | ||
} | ||
// std::cin.get(); | ||
return 0; | ||
} |
Oops, something went wrong.