I/O in C++

21
I/O in C++ October 7, 2008. Junaed Sattar

description

October 7, 2008. Junaed Sattar. I/O in C++. Stream I/O. a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output streams: from the program note I use plural one program can have multiple I/O streams associated and vice-versa. Input/Output. - PowerPoint PPT Presentation

Transcript of I/O in C++

I/O in C++

October 7, 2008.Junaed Sattar

Stream I/O

a stream is a flow of bytes/characters/ints or any type of data

input streams: to the program

output streams: from the program

note I use plural one program can have multiple I/O streams

associated

and vice-versa

Input/Output

Console based, no GUI

standard streams: cin: standard input

cout: standard output

cerr: standard error

Extraction/Insertion

cout << “Hello world!”;

cout << “The value of i is “ << i << endl;

//endl puts a new line

cout << “Please enter your name: “;

string name;

cin >> name;

What are cin and cout?

Stream classes

Classes have methods, as we know so does cin and cout

some common to all I/O stream classes in C++

File I/O, binary/text mode I/O, console I/O

One example

cin inputs ints, chars, null-terminated strings, string objects but terminates when encounters space (ascii

character 32)

workaround?

use the “get” method

Snippet

char tData[100];

// This is a method in C++ istream classes for

// inputting text

//including spaces

cin.get( tData, 99 );

// or

cin.get(tData,99,'\n');

Inputting “i am oh so cool”

cin.get gets the entire line

just cin will get “I” space termination

Or,

Use the getline function

getline( cin, name );

File I/O

Reading from or writing to files on disk ifstream and ofstream classes

dedicated for input and output respectively

or, use fstream

Example Files Program(filesdemo)

ofstream myofile;

myofile.open( “sample.txt” );

myofile << “This is a sample line I'm writing\n”;

myofile.close();

...

ifstream myifile;

myifile.open( “sample.txt” );

string oneLine;

getline( myifile, oneLine );

cout << oneLine;

myifile.close();

Read/Write to files (files1/2)

Similar to how we use cin and cout remember, these are I/O streams too

myfile is a file stream object, then: to write an int:

int i = 10;myfile << i;

to read an int: int i;

myfile >> i;

Binary files

As opposed to text files, they are unformatted as ascii. text files stores everything as ascii text strings

even numbers

binary files do not

Example: consider outout of the program in the previous slide

Difference?

Example program Accepts student ID (I input 1010)

Accepts name (I input Junaed)

Accepts CGPA (I input 4.5)

Save into two files, as text and binary

Storage

TEXT FILE

BINARY FILE

Binary files

Files by default are text

Different methods to write and read requires casting (we'll see casting soon)

different data format

If time permits, we'll revisit

Failures?

If open fails? Check before use if( !myifile ){ cerr << “Cannot open file!”; exit(1);}

End of file? while( myifile.fail() ){ //do your operations here}

Random vs Sequential

Random access files nonsequential,

as a result faster access times,

content must be suitable for random access for example. not on network streams! or console input

File “heads”

Access positions

one each for read and write

hence two methods: seekg (as in “get”) for reading

seekp (as in “put”) for writing

ifstreams have seekg

ofstreams have seekp

seeking

seekg( position, mode) //(same for seekp)

position is a long integer signed offset

mode can be ios::beg: from the beginning

ios::end from the end

ios::cur from current position

telling

tellg and tellp returns as long integer, the position of the get and

put positions, respectively

example seeks

file.seekg( 20L, ios::beg );

file.seekp( -100L, ios::cur );

long pPosition = file.tellp();

long gPosition = file.tellg();