1 of 45

Managing I/O Console �using C++

2 of 45

C++ Stream Classes�

  • Stream and stream classes : to implement I/O operations with the console and disk files.
  • It is a sequence of bytes(or interface)..
  • It acts as a source from which input data can be obtained.
  • or as destination to which the output can be sent.
  • Source stream is called Input stream.
  • Destination stream is called Output stream.

3 of 45

  • Data in the Input stream can come from Keyboard etc.
  • Similarly, data in the output stream can go to the screen.
  • C++ provides standard iostream library to operate with streams.
  • Cin and cout are the predefined streams .

4 of 45

  • If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called input operation .

  • and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called output operation.

5 of 45

  • istream is a general purpose input stream. cin is an example of an istream.
  • ostream is a general purpose output stream. cout is the example of ostream.
  • ifstream is an input file stream. It is a special kind of an istream that reads in data from a data file.
  • ofstream is an output file stream. It is a special kind of ostream that writes data out to a data file.

6 of 45

  • C++ provides the following classes to perform output and input of characters to/from files:��ofstream: Stream class to write on files
  • ifstream: Stream class to read from files
  • fstream: Stream class to both read and write from/to files.

7 of 45

Console I/O operations

There are mainly two types of console I/O operations form:

  • Unformatted console input output
  • Formatted console input output

8 of 45

Unformatted input output operations

The following are operations of unformatted input / output operations:

A) void get()

  • It is a method of cin object used to input a single character from keyboard. But its main property is that it allows wide spaces and newline character.
  • Syntax:

char c=cin.get();

9 of 45

Example

#include<iostream>

using namespace std;

int main()

{

char c=cin.get(); �cout<<c<<endl;

return 0;

}

10 of 45

void put()

  • It is a method of cout object and it is used to print the specified character on the screen or monitor.
  • Syntax:

cout.put(variable / character);

11 of 45

Example

#include<iostream>

using namespace std;

int main()

{

char c=cin.get();

cout.put(c); //Here it prints the value of variable c;

cout.put('c'); //Here it prints the character 'c';

return 0;

}

12 of 45

getline(char ,int size)

  • This is a method of cin object and it is used to input a string with multiple spaces.
  • Syntax:

char x[30];

cin.getline(x,30);

13 of 45

Example

#include<iostream>

using namespace std;

int main()

{

cout<<"Enter name :";

char c[10];

cin.getline(c,10); //It takes 10 charcters as

input

cout<<c<<endl;

return 0;

}

14 of 45

write(char , int n)

  • It is a method of cout object. This method is used to write n character from buffer variable.

  • Syntax:

cout.write(x,2);

15 of 45

Example

#include<iostream>

using namespace std;

int main()

{

cout<<"Enter name : ";

char c[10];

cin.getline(c,10); //It takes 10 charcters as input;

cout.write(c,9); //It reads only 9 character from buffer c;

return 0;

}

16 of 45

cin

  • It is the method to take input any variable / character / string.

  • Syntax:

cin>>variable / character / String / ;

17 of 45

Example

#include<iostream>

using namespace std;

int main()

{

int num;

char ch;

string str;

cout<<"Enter Number"<<endl;

cin>>num; //Inputs a variable;

cout<<"Enter Character"<<endl;

cin>>ch; //Inputs a character;

cout<<"Enter String"<<endl;

cin>>str; //Inputs a string;

return 0;

}

18 of 45

cout

  • This method is used to print variable / string / character.

Syntax:

cout<< variable / charcter / string;

19 of 45

Example

#include<iostream>

using namespace std;

int main()

{

int num=100;

char ch='X';

string str="Deepak";

cout<<"Number is "<<num<<endl; //Prints value of variable;

cout<<"Character is "<<ch<<endl; //Prints character;

cout<<"String is "<<str<<endl; //Prints string;

return 0;

}

20 of 45

Formatted console input output operations

21 of 45

  • In formatted console input output operations we uses functions to make output in perfect alignment. 

  • C++ provides many function to convert any file into perfect aligned format.

  • These functions are available in header file <iomanip>. 

  • iomanip refers input output manipulations.

22 of 45

width(n)

  • This function is used to set width of the output.

  • Syntax:

cout<<setw(int n);

23 of 45

Example

#include<iostream>

#include<iomanip>

using namespace std;

int main()

{

int x=10;

cout<<setw(20)<<x;

return 0;

}

24 of 45

fill(char)

  • This function is used to fill specified character at unused space.

  • Syntax:

cout<<setfill('character')<<variable;

25 of 45

Example

#include<iostream>

#include<iomanip>

using namespace std;

int main()

{

int x=10;

cout<<setw(20);

cout<<setfill('#')<<x;

return 0;

}

26 of 45

precison(n)

  • This method is used for setting floating point of the output.

  • Syntax:

cout<<setprecision('int n')<<variable;

27 of 45

#include<iostream>

#include<iomanip>

using namespace std;

int main()

{

float x=10.12345;

cout<<setprecision(5)<<x;

return 0;

}

28 of 45

Working with Files

  • The data are stored in the secondary device using the concept of files.

  • Files are the collection of related data stored in a particular area on the disk.

  • Programs can be written to perform read and write operations on these files.

29 of 45

Working with files generally requires the following kinds of data communication methodologies:

  • Data transfer between console units.
  • Data transfer between the program and the disk file.
  • We use the header file <fstream>

30 of 45

File Handling Classes

  • ofstream: It represents output Stream and this is used for writing in files.

  • ifstream: It represents input Stream and this is used for reading from files.

  • fstream: It represents both output Stream and input Stream. So it can read from files and write to files.

31 of 45

Operations in File Handling

  • Creating a file: open()
  • Reading data: read()
  • Writing new data: write()
  • Closing a file: close()

32 of 45

Opening & Closing a file in C++

33 of 45

Opening a file

Files can be opened in two ways. They are:

  • Using constructor function of the class.

- Useful when we use only one file.

  • Using member function open of the class.

- Manage multiple files using one stream.

34 of 45

Using constructor function of the class

The syntax of opening a file in C++ is:

open (filename, mode);

There are some mode flags used for file opening. These are:

  • ios::app: append mode
  • ios::ate: open a file in this mode for read/write controlling to the end of the file
  • ios::in: open file in this mode for reading
  • ios::out: open file in this mode for writing
  • ios::trunk: when any file already exists, its contents will be truncated before file opening

35 of 45

  • When any C++ program terminates, it automatically flushes out all the streams, releases all the allocated memory and closes all the opened files.
  • But it is good to use the close() function to close the file related streams and it is a member of ifsream, ofstream and fstream objects.
  • The structure of using this function is:

void close();

36 of 45

Reading from and writing to file

  • While doing C++ program, programmers write information to a file from the program using the stream insertion operator (<<) and reads information using the stream extraction operator (>>).
  • The only difference is that for files programmers need to use an ofstream or fstream object instead of the cout object and ifstream or fstream object instead of the cin object.

37 of 45

Opening file using constructor

  • This involves the following steps:-
  • Create a file stream object to manage the stream using the appropriate class.
  • Initialize the file object with desired filename.

ofstream outfile(“result”);

outfile<<<“total”;

ifstream infile(“data”);

infile>>number;

38 of 45

Working with Single file

#include<fstream.h>

void main()

{

ofstream outf(“item”);

cout<<“enter name”;

char name[30];

cin>>name;

outf<<name;

float cost;

cin>>cost;

outf<<cost;

outf.close();

ifstream inf(“item”);

inf>>name;

inf>>cost;

cout<<name;

cout<<cost;

inf.close();

}

39 of 45

  • When a file is opened for writing only, a new file is created if there is no file of that name.

  • If file by that name exists already, then its contents are deleted and file is presented as a clean file.

  • Without losing the original contents, open an existing file for updating.

40 of 45

Opening file using open() function

  • Function open() can be used open multiple files that uses the same stream object.

fstream stream_obj;

stream_obj.open(“file_name”);

  • First file is closed before opening the second one. This is necessary because stream can be connected to only one file at a time.

41 of 45

Example

ofstream outfile;

outfile.open(“data1”);

--------------

---------------

outfile.close();

outfile.open(“data2”);

--------------

---------------

outfile.close();

42 of 45

Create file with Open() function

#include<fstream.h>

void main()

{

ofstream fout;

fout.open(“counting”);

fout<<“US”;

fout<<“UK”;

fout<<“india”;

fout.close();

fout.open(“capital”);

fout<<“washington”;

fout<<“london”;

fout<<“delhi”;

fout.close();

  • //reading the files(PTO)

43 of 45

int N=80;

char line[N];

ifstream fin;

fin.open(“country”);

while(fin)

{

fin.getline(line,N);

cout<<line;

}

fin.close();

fin.open(“capital”);

while(fin)

{

fin.getline(line,N);

cout<<line;

}

fin.close();

}

44 of 45

Detecting end-of-file

  • While(fin) //ifstream object

  • Another method:
  • if(fin.eof()!=0);
  • Eof() is a member of ios class.
  • This statement terminates the program on reaching the end of file.

45 of 45

More about open file modes

  • object.open(“filename”); ///one argument
  • open() can take 2 arguments.

object.open(“filename”,mode);

  • Member function contain default values for second argument and therefore they use default values in the absence of actual values.
  • Ios::in
  • Ios::out