Вы находитесь на странице: 1из 3

/*

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

#include <iostream>
using namespace std;

int main() {
cout << cin.rdbuf();
}
//obviously this only copies from cin into cout

//two ways to copy. another one would be:


cin >> cout.rdbuf();

//two ways of copying a file using an internal buffer

/* Seeking within a file */


//tellp and tellg - these retrieve our current position
//p = put, g = get
//conceptually there's a conception for writing and another for reading
//tellg tells us the location for reading
//tellp tells us the location for writing

//In the most general case, these 2 positions can be different.


//For files, there is only 1 position

//how do you get the current position?


ios::pos_type pos = is.tellg();
//is is an inputstream
if (pos == ios::pos_type(-1)) { //tellg failed, some error handling

}
//notice it's defined in ios, not ios_base

seekp //has 2 versions: absolute seeking and relative seeking


seekg //also 2 versions

//example of absolute seeking


if (!is.seekg(pos)) {
...
}

//example of relative seeking


seekg(ios::off_type, ios_base::seekdir)
// offset type seekdir type
// three possible values for ios_base:
// 1. ios_base::beg //relative to beginning
// 2. ios_base::cur //relative to current position
// 3. ios_base::end //relative to end

if(os.seekp(ios::off_type(-5)), ios_base::cur) {
// try to move back 5 bytes
...
}
//For stringstreams, the "get" & "put" positions are independent.

#include <iostream>
#include <sstream>
#include <string>
void read_int(istream& is) {
using namespace std;
int n;
if (is >> n)
cout << n << endl;
else
cout << "can't read an int" << endl;
}

void read_word(istream& is) {


string n;
if (is >> n)
cout << n << endl;
else
cout << "can't read a word" << endl;
}

int main() {
stringstream ss("12hello world"); //2 spaces inside string hello world

read_int(ss);
read_int(ss);
read_word(ss);
ss.clear();
read_word(ss);
ss << "goodbye!";
read_word(ss);
if (!ss.seekg(ios::off_type(-3), ios_base::cur))
cout << "can't seek" << endl;
read_word(ss);
read_word(ss);
}
/** pay attention to the state of the strings at various points */
//everything fails when string is not in a good state

/* Miscellaneous info on streams */


int n;
cin >> hex >> n; //read hexadecimal
char word[20];
//we don't want to overflow the buffer:
cin >> setw(20) >> word; //will read at most 19 characters packaged with \0
cin >> ws; //eat up the whitespace
cin >> noskipws;
skipws;
iss >> c >> n

//...And this is the story of streams.

/* Inheritance */
ios_base
/\
|
basic_ios<>
/\ /\
___________| \______________
basic_istream<> basic_ostream<>
/\ /\ /\ /\ /\ /\
| | |____ ________| | \
| | | | | \
| | basic_iostream<> | \
| | /\ /\ | \
| | | \ | \
basic_ifstream<> | | \ | \
ifstream | | \ basic_ofstream<> |
| | \ |
basic_istringstream<> | \ |
istringstream | \ |
basic_fstream<> | basic_ostringstream<>
fstream |
|
basic_stringstream<>

//ios_base::beg can also be referred to as ios::beg

ios_base::in // ---> ifstream::in //doesn't matter which prefix you use

/* Default arguments */
//we can specify default values for certain parameters when
// _declaring_ a function

void f(int, int = 1, int = 2);


//default arguments
//this function can be called with 1, 2 or 3 arguments.
f(4); //same as (4, 1, 2);
f(4, 3); //same as f(4,3,2);

//If a default value is given for a parameter, all later parameters


// must have default values
void g(int, int = 1, int); //invalid

//when calling the function, if a value is given for a parameter,


//all earlier ones must be given values
f( 0, , 3); //invalid

Вам также может понравиться