You are on page 1of 3

/*

* File: 20160916_CPP_L05.cpp.
* Title: 2016-09-16 C++ Lesson 05
* Author: Renato Montes
* Description: Notes for Albert Wei's C++ class
*/

/* ostringstream */
#include <sstream>
//allows us to print a string

//easy example

ostringstream oss //no semicolon?


int n = 123;
oss << hex << n << oct << ' ' << n;
cout << oss.str() << endl;
//don't know state of cout if passed from a function
//the advantage of ostringstream is that we don't need to change the
// format state of cout directly

/* File I/O */

#include <fstream>
//ifstream/ofstream/fstream

//example: summing integers stored in a file


int main() {
ifstream in("data.txt"); //calls ctor
//can also write it this way: int n(123); //exactly the same thing
// int is the type
// n is the variable
// 123 is the arguments
// can also be: int n{123}
if(!in) {
cerr << "cannot open file" << endl;
return 1;
}
int n, sum = 0;
while (in >> n)
sum += n;
cout << sum << endl;
}
//don't need to close the file, because of C++'s destructor, which
// automatically closes the file
//ctor of in automatically closes the file
//when in goes out of scope, the file is automatically closed

/* How about binary files? */

//ctor can take a 2nd argument that specifies the open mode
//type: ios_base::openmode
//possible values: ios_base::in, out, app, trunc, ate, binary
// app append
// trunc truncate
/** TODO find what ate is */ //Answer: ate = at end
//We can combine them using bitwise-or
ifstream in("data", ios_base::in|ios_base::binary);
//when there's no 2nd argument it's text mode basically
//if you specify the 2nd argument you need to specify in explicitly

//not all combinations are valid!


//the following are valid:
// in out trunc app stdio equivalent
// Y r
// Y w
// Y Y w
// Y a
// Y Y a
// Y Y r+
// Y Y Y w+
// Y Y a+
// Y Y Y a+
//There are 6 corresponding binary modes.

//Example: program to copy a file


void copy(istream&, ostream&);
int main(int argc, char *argv[]) {
if(argc != 3) {
cerr << "usage: " << argv[0] << " [sourcefile][destinationfile]\n";
return 1;
}
ifstream in(argv[1], ios_base::in|ios_base::binary);
if (!in) {
cerr << "unable to open '" << argv [1] << "' for reading\n";
return 2;
}
//now we need to open the other file for writing:
ofstream out(argv[2], ios_base::out|ios_base::binary);
if(!out) {
cerr << "unable to open '" << argv[2] << "' for writing\n";
return 3;
}
copy(in, out);
}//in and out are automatically closed

//there are different ways of copying: char by char, line by line,


// block by block

//copying char by char


void copy(istream& is, ostream& os) {
char c;
while(is.get(c))
os.put(c);
}
//get(c) takes a character by reference, hence you can change the c
// passed in line 94 here

//copying line by line (non-portable)


void copy(istream& is, ostream& os) {
string line;
while(getline(is, line))
//how do we tell there's no newline char at the end,
// encountering end-of-file
if(is.eofc())
os << line //no semicolon?
else
os << line << endl;
}

//copying block by block


char buffer[BUFSIZE]
is.read(buffer, BUFSIZE)
//this is how you read from the input stream
//it reads up to BUFSIZE chars from newline
// & stores them in the bugger.
// On end-of-file, it sets both eofbit & failbit

//os.write(buffer, n) - write n bytes of buffer to os


//is.gcount() - returns the number of bytes read by the last
// unformatted input
//unformatted input is sort of more low level
//when trying to read double or float that's unformatted input
//call gcount() to figure out how many bytes have been read

//combining all this information above:


void copy(istream& is, ostream& os) {
char buffer[BUFSIZE];
streamsize nbytes; //signed integral type for portability
while(1) {
is.read(buffer, BUFSIZE);
if ((nbytes = is.gcount()) > 0)
os.write(buffer, nbytes);

if(!is)
break;
}
}

You might also like