sort and count numbers

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 11 Exercise 16
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PPP
//	Chapter 11 Exercise 16

#include "std_lib_facilities.h"

int main()
try
{
	cout << "enter name for input file" << endl;
	string iname = "input/";
	string itemp;
	cin >> itemp;
	iname += itemp;
	ifstream ifs{ iname };
	if (!ifs) { cout << "could not open input file." << endl; }

	cout << "enter name for output file" << endl;
	string oname = "output/";
	string otemp;
	cin >> otemp;
	oname += otemp;
	ofstream ofs{ oname };
	if (!ofs) { cout << "could not open output file." << endl; }

	vector<double> nums;
	for (double d; ifs >> d;) {
		nums.push_back(d);
	}

	sort(nums);

	for (int i = 0; i < nums.size(); i++) {
		cout << i << " " << nums[i] << endl;
	}

	int count = 1;

	for (int i = 1; i < nums.size(); i++) {
		if (nums[i] == nums[i - 1]) {
			count++;
		}
		else {
			ofs << nums[i - 1];
			if (count > 1) {
				ofs << " " << count << endl;
				count = 1;
			}
			else {
				ofs << endl;
			}
		}
		if (i == nums.size() - 1 && nums[i] == nums[i - 1]) {
			if (count == 1) {
				count++;
			}
			ofs << nums[i];
			ofs << " " << count << endl;
			ofs << endl;
		}
	}

	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
enter name for input file
p409_16_random_num.txt
enter name for output file
p409_16_random_num_out.txt
Please enter a character to exit
Input-File: p409_16_random_num.txt
58
32
50
80
54
13
24
82
52
17
17
8
87
12
35
42
88
72
46
91
69
60
84
55
99
21
6
25
53
13
75
83
94
2
71
35
85
50
71
99
60
47
68
29
100
54
45
66
4
98
57
13
23
3
8
9
47
67
17
52
5
60
73
28
18
45
5
7
99
78
78
17
3
87
37
33
83
92
83
99
45
75
28
73
66
44
11
73
9
24
29
39
52
99
24
95
8
91
90
99
100
Input-File: p409_16_random_num_out.txt
2
3 2
4
5 2
6
7
8 3
9 2
11
12
13 3
17 4
18
21
23
24 3
25
28 2
29 2
32
33
35 2
37
39
42
44
45 3
46
47 2
50 2
52 3
53
54 2
55
57
58
60 3
66 2
67
68
69
71 2
72
73 3
75 2
78 2
80
82
83 3
84
85
87 2
88
90
91 2
92
94
95
98
99 6
100 2

number matrix

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 11 Exercise 15
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PPP
//	Chapter 11 Exercise 15

#include "std_lib_facilities.h"

int main()
try
{
	cout << "enter name for input file" << endl;
	string iname = "input/";
	string itemp;
	cin >> itemp;
	iname += itemp;
	ifstream ifs{ iname };
	if (!ifs) { cout << "could not open input file." << endl; }

	cout << "enter name for output file" << endl;
	string oname = "output/";
	string otemp;
	cin >> otemp;
	oname += otemp;
	ofstream ofs{ oname };
	if (!ofs) { cout << "could not open output file." << endl; }

	int width = 20;
	int counter = 0;

	for (double d; ifs >> d;) {
		ofs << setw(width) << scientific << d;
		counter++;
		if (counter == 4) {
			ofs << endl;
			counter = 0;
		}
	}

	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
enter name for input file
p409_15_random_num.txt
enter name for output file
p409_15_random_num_out.txt
Input-File: p409_15_random_num.txt
58
32
50
80
54
13
24
82
52
17
17
8
87
12
35
42
88
72
46
91
69
60
84
55
99
21
6
25
53
13
75
83
94
2
71
35
85
50
71
99
60
47
68
29
100
54
45
66
4
98
57
13
23
3
8
9
47
67
17
52
5
60
73
28
18
45
5
7
99
78
78
17
3
87
37
33
83
92
83
99
45
75
28
73
66
44
11
73
9
24
29
39
52
99
24
95
8
91
90
99
Input-File: p409_15_random_num_out.txt
       5.800000e+01        3.200000e+01        5.000000e+01        8.000000e+01
        5.400000e+01        1.300000e+01        2.400000e+01        8.200000e+01
        5.200000e+01        1.700000e+01        1.700000e+01        8.000000e+00
        8.700000e+01        1.200000e+01        3.500000e+01        4.200000e+01
        8.800000e+01        7.200000e+01        4.600000e+01        9.100000e+01
        6.900000e+01        6.000000e+01        8.400000e+01        5.500000e+01
        9.900000e+01        2.100000e+01        6.000000e+00        2.500000e+01
        5.300000e+01        1.300000e+01        7.500000e+01        8.300000e+01
        9.400000e+01        2.000000e+00        7.100000e+01        3.500000e+01
        8.500000e+01        5.000000e+01        7.100000e+01        9.900000e+01
        6.000000e+01        4.700000e+01        6.800000e+01        2.900000e+01
        1.000000e+02        5.400000e+01        4.500000e+01        6.600000e+01
        4.000000e+00        9.800000e+01        5.700000e+01        1.300000e+01
        2.300000e+01        3.000000e+00        8.000000e+00        9.000000e+00
        4.700000e+01        6.700000e+01        1.700000e+01        5.200000e+01
        5.000000e+00        6.000000e+01        7.300000e+01        2.800000e+01
        1.800000e+01        4.500000e+01        5.000000e+00        7.000000e+00
        9.900000e+01        7.800000e+01        7.800000e+01        1.700000e+01
        3.000000e+00        8.700000e+01        3.700000e+01        3.300000e+01
        8.300000e+01        9.200000e+01        8.300000e+01        9.900000e+01
        4.500000e+01        7.500000e+01        2.800000e+01        7.300000e+01
        6.600000e+01        4.400000e+01        1.100000e+01        7.300000e+01
        9.000000e+00        2.400000e+01        2.900000e+01        3.900000e+01
        5.200000e+01        9.900000e+01        2.400000e+01        9.500000e+01
        8.000000e+00        9.100000e+01        9.000000e+01        9.900000e+01

char classification [counting letters, points, commas and white-spaces]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 11 Exercise 14
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PPP
//	Chapter 11 Exercise 14

#include "std_lib_facilities.h"

int main()
try
{
	cout << "enter name for input file" << endl;
	string iname = "input/";
	string itemp;
	cin >> itemp;
	iname += itemp;
	ifstream ifs{ iname };
	if (!ifs) { cout << "could not open input file." << endl; }

	int letters = 0;
	int pointComma = 0;
	int wss = 0;

	for (char ch; ifs.get(ch);) {
		if (isspace(ch)) {
			wss++;
		}
		else if (ch == ',' || ch == '.') {
			pointComma++;
		}
		else {
			letters++;
		}
	}

	cout << "Letters: " << letters << endl;
	cout << "Point's & Comma's: " << pointComma << endl;
	cout << "Whitespaces: " << wss << endl;

	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
enter name for input file
p409_14_blind_text.txt
Letters: 1106
Point's & Comma's: 63
Whitespaces: 199
Input-File: p409_14_blind_text.txt
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.
Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.
In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.
Nullam dictum felis eu pede mollis pretium. Integer tincidunt.
Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.
Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim.
Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet.
Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui.
Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum.
Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus.
Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt.
Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,

reverse word order in text file

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 11 Exercise 13
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PPP
//	Chapter 11 Exercise 13

#include "std_lib_facilities.h"

int main()
try
{
	cout << "enter name for input file" << endl;
	string iname = "input/";
	string itemp;
	cin >> itemp;
	iname += itemp;
	ifstream ifs{ iname };
	if (!ifs) { cout << "could not open input file." << endl; }

	cout << "enter name for output file" << endl;
	string oname = "output/";
	string otemp;
	cin >> otemp;
	oname += otemp;
	ofstream ofs{ oname };
	if (!ofs) { cout << "could not open output file." << endl; }

	string word;
	vector<string> words;

	for (char ch; ifs.get(ch);) {
		if (isspace(ch)) {
			words.push_back(word);
			word = "";
			word += ch;
			words.push_back(word);
			word = "";
		}
		else {
			word += ch;
		}
	}
	words.push_back(word);

	for (int i = 1; i < words.size() + 1; i++) {
		ofs << words[words.size() - i];
	}

	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
enter name for input file
p409_13_blind_text.txt
enter name for output file
p409_13_blind_text_out.txt
Please enter a character to exit
Input-File: p409_13_blind_text.txt
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.
Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.
In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.
Nullam dictum felis eu pede mollis pretium. Integer tincidunt.
Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.
Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim.
Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet.
Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui.
Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum.
Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus.
Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt.
Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
Output-File: p409_13_blind_text_out.txt
nunc, cursus velit augue sodales, bibendum eget leo consequat, Sed magna. sagittis sodales Donec nibh. amet sit mauris fringilla Sed leo. Duis
tincidunt. faucibus eros eget orci amet sit Etiam ante. quis Nullam faucibus. venenatis libero ut sapien vitae Donec
tempus. tincidunt ante et odio nec Maecenas lorem. id, hendrerit pulvinar, luctus vel, blandit nunc, quam Nam
ipsum. sed neque sem adipiscing amet sit libero, semper quam sem rhoncus, condimentum eget tellus tempus, Maecenas rhoncus. Etiam
dui. eget Nam nisi. ultricies ullamcorper Curabitur augue. vel nisi ultricies Etiam imperdiet. Aenean rutrum. Quisque
laoreet. varius metus ut nulla viverra Phasellus tellus. a, feugiat quis, viverra in, dapibus ante, lorem Aliquam
enim. ac, eleifend vitae, consequat eu, porttitor ligula, leo Aenean
tellus. eleifend vulputate Aenean nisi. semper elementum Vivamus dapibus. Cras
tincidunt. Integer pretium. mollis pede eu felis dictum Nullam
justo. vitae, venenatis a, imperdiet ut, rhoncus justo, enim In
arcu. eget, vulputate nec, aliquet vel, fringilla justo, pede Donec
enim. quis massa consequat Nulla sem. quis, pretium eu, pellentesque nec, ultricies felis, quam Donec
mus. ridiculus nascetur montes, parturient dis magnis et penatibus natoque sociis Cum massa. Aenean
dolor. eget ligula commodo Aenean elit. adipiscing consectetuer amet, sit dolor ipsum Lorem

reverse chars of string in text file

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 11 Exercise 12
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PPP
//	Chapter 11 Exercise 12

#include "std_lib_facilities.h"

void reverseWord(string& w) {
	string temp;
	for (int i = 1; i < w.size() + 1; i++) {
		temp += w[w.size() - i];
	}
	w = temp;
}

void readWriteFile() {
	string iname = "input/p409_12_blind_text.txt";
	ifstream ifs{ iname };
	string oname = "output/p409_12_blind_text_out.txt";
	ofstream ofs{ oname };

	while (true) {
		if (ifs.eof()) {
			break;
		}
		string temp;
		string tempReverse;
		char ch;
		if (getline(ifs,temp)) {
			cout << temp << endl;
			string word;
			for (int i = 0; i < temp.size(); i++) {
				if (isalpha(temp[i])) {
					word += temp[i];
				}
				else {
					if (word.size() != 0) {
						reverseWord(word);
						tempReverse += word;
						word = "";
					}
					tempReverse += temp[i];
				}
				if (i == temp.size() - 1) {
					tempReverse += '\n';
				}
			}
		}
		ofs << tempReverse;		
	}
}

int main()
try
{
	readWriteFile();
	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
Please enter a character to exit
Input-File: p409_12_blind_text.txt
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.
Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.
In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.
Nullam dictum felis eu pede mollis pretium. Integer tincidunt.
Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.
Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim.
Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet.
Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui.
Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum.
Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus.
Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt.
Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
Output-File: p409_12_blind_text_out.txt
meroL muspi rolod tis tema, reutetcesnoc gnicsipida tile. naeneA odommoc alugil tege rolod.
naeneA assam. muC siicos euqotan subitanep te singam sid tneirutrap setnom, rutecsan sulucidir sum.
cenoD mauq silef, seicirtlu cen, euqsetnellep ue, muiterp siuq, mes. alluN tauqesnoc assam siuq mine.
cenoD edep otsuj, allignirf lev, teuqila cen, etatupluv tege, ucra.
nI mine otsuj, sucnohr tu, teidrepmi a, sitanenev eativ, otsuj.
malluN mutcid silef ue edep sillom muiterp. regetnI tnudicnit.
sarC subipad. sumaviV mutnemele repmes isin. naeneA etatupluv dnefiele sullet.
naeneA oel alugil, rotittrop ue, tauqesnoc eativ, dnefiele ca, mine.
mauqilA merol etna, subipad ni, arreviv siuq, taiguef a, sullet. sullesahP arreviv allun tu sutem suirav teeroal.
euqsiuQ murtur. naeneA teidrepmi. maitE seicirtlu isin lev eugua. rutibaruC reprocmallu seicirtlu isin. maN tege iud.
maitE sucnohr. saneceaM supmet, sullet tege mutnemidnoc sucnohr, mes mauq repmes orebil, tis tema gnicsipida mes euqen des muspi.
maN mauq cnun, tidnalb lev, sutcul ranivlup, tirerdneh di, merol. saneceaM cen oido te etna tnudicnit supmet.
cenoD eativ neipas tu orebil sitanenev subicuaf. malluN siuq etna. maitE tis tema icro tege sore subicuaf tnudicnit.
siuD oel. deS allignirf siruam tis tema hbin. cenoD selados sittigas angam. deS tauqesnoc, oel tege mudnebib selados, eugua tilev susruc cnun,

string split 1.1

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 11 Exercise 11
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PPP
//	Chapter 11 Exercise 11

#include "std_lib_facilities.h"

vector<string> split(const string& s, const string& w) {
	vector<string> tempVec;
	string temp;	
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == w[0] && w.size() > 1) {
			int count = 1;
			for (int j = 1; j < w.size(); j++) {
				if (s[i + j] == w[j]) {
					count++;
				}
			}
			if (count == w.size()) {
				tempVec.push_back(temp);
				temp = "";
				i += count - 1;
			}
			else if(!isspace(s[i])){
				temp += s[i];
			}
		}
		else if(s[i] == w[0] && w.size() == 1){
			tempVec.push_back(temp);
			temp = "";
		}
		else if (isspace(s[i]) && temp != "") {
			tempVec.push_back(temp);
			temp = "";
		}
		else if(!isspace(s[i])) {
			
			temp += s[i];
		}
		if (i == s.size() - 1) {
			tempVec.push_back(temp);
		}
	}
	return tempVec;
}

int main()
try
{
	vector<string> myVec;
	string temp = "Hello World, how arowe you today?";
	string add = "o";
	myVec = split(temp, add);

	for (int i = 0; i < myVec.size(); i++) {
		cout << myVec[i] << endl;
	}

	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
Hell
W
rld,
h
w
ar
we
y
u
t
day?
Please enter a character to exit

string split 1.0

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 11 Exercise 10
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PPP
//	Chapter 11 Exercise 10

#include "std_lib_facilities.h"

vector<string> split(const string& s) {
	vector<string> tempVec;
	string temp;

	for (int i = 0; i < s.size(); i++) {
		if (isspace(s[i])) {
			tempVec.push_back(temp);
			temp = "";
		}
		else {
			temp += s[i];
		}
		if (i == s.size() - 1) {
			tempVec.push_back(temp);
		}
	}
	return tempVec;
}

int main()
try
{
	vector<string> myVec;
	string temp = "Hello World, how are you today?";
	myVec = split(temp);	

	for (int i = 0; i < myVec.size(); i++) {
		cout << myVec[i] << endl;
	}

	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
Hello
World,
how
are
you
today?
Please enter a character to exit

binary write

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 11 Exercise 9 Write
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PPP
//	Chapter 11 Exercise 9 Write

#include "std_lib_facilities.h"

int main()
try
{
	//open an istream for binary input from a file:
	cout << "Please enter input file name\n";
	string iname = "output/";
	string in;
	cin >> in;
	iname += in;	
	ifstream ifs{ iname }; //note: stream mode
											 //binary tells the stream not to try anything clever with the bytes

	if (!ifs) error("can't open input file", iname);

	//open an ostream for binary output to a file:
	cout << "Please enter output file name\n";
	string oname = "output/";
	string on;
	cin >> on;
	oname += on;
	ofstream ofs{ oname }; //note: stream mode
											 //binary tells the stream not to try anything clever with the bytes

	vector<int> v;
	for (int x; ifs >> x;) { //note: reading bytes
		v.push_back(x);
	}

	for (int x : v) { //char x : v
		ofs.write(as_bytes(x), sizeof(int)); //note: writing bytes
	}

	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
Please enter input file name
p409_9_read_out.txt
Please enter output file name
p409_9_write_out.txt
Please enter a character to exit
Input-File: p409_9_read_out.txt
1684955424
544174880
1948283753
Output-File: p409_9_write_out.txt
 and so is t

binary read

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 11 Exercise 9 Read
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PPP
//	Chapter 11 Exercise 9 Read

#include "std_lib_facilities.h"

int main()
try
{
	//open an istream for binary input from a file:
	cout << "Please enter input file name\n";

	string in;
	string iname = "input/";
	cin >> in;
	iname += in;
	
	ifstream ifs{ iname, ios_base::binary }; //note: stream mode
	//binary tells the stream not to try anything clever with the bytes
	
	if (!ifs) error("can't open input file", iname);
	else {
		cout << "file opened" << endl;
	}

	//open an ostream for binary input from a file:
	cout << "Please enter output file name\n";

	string out;
	string oname = "output/";
	cin >> out;
	oname += out;

	ofstream ofs{ oname };
	
	vector<int> v;

	//read from binary file:
	for (int x; ifs.read(as_bytes(x), sizeof(int));) { //note: reading bytes
		v.push_back(x);
	}
		
	//cout binary file:
	for (int x : v) {
		cout << x << endl;
		ofs << x << endl;
	}	

	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
Please enter input file name
ch10_9_input_2.txt
file opened
Please enter output file name
p409_9_read_out.txt
1684955424
544174880
1948283753
Please enter a character to exit
Input-File: ch10_9_input_2.txt
 and so is this
Output-File: p409_9_read_out.txt
1684955424
544174880
1948283753

binary input/output

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 11 Exercise 9 IO
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PPP
//	Chapter 11 Exercise 9 IO

#include "std_lib_facilities.h"

int main()
try
{
	//open an istream for binary input from a file:
	cout << "Please enter input file name\n";
	string iname;
	cin >> iname;
	ifstream ifs{ iname.c_str(), ios_base::binary }; //note: stream mode
											 //binary tells the stream not to try anything clever with the bytes

	if (!ifs) error("can't open input file", iname);

	//open an ostream for binary output to a file:
	cout << "Please enter output file name\n";
	string oname;
	cin >> oname;
	ofstream ofs{ oname }; //note: stream mode // oname.c_str(), ios_base::binary
											 //binary tells the stream not to try anything clever with the bytes

	if (!ofs) error("can't open output file", oname);
	vector<int> v;

	//read from binary file:
	for (int x; ifs.read(as_bytes(x), sizeof(int));) { //note: reading bytes
		v.push_back(x);
		//cout << x << endl;
	}

	//...do something...

	//write to binary file:
	for (int x : v) { //char x : v
		ofs.write(as_bytes(x), sizeof(int)); //note: writing bytes
	}

	//return 0;
	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
Please enter input file name
ch10_9_input_1.txt
Please enter output file name
ch10_9_output_1.txt
Please enter a character to exit
Input-File: ch10_9_input_1.txt
this is a test
Output-File: ch10_9_output_1.txt
this is a te