Managing I/O Console �using C++�
C++ Stream Classes�
Console I/O operations
There are mainly two types of console I/O operations form:
�Unformatted input output operations�
The following are operations of unformatted input / output operations:
A) void get()
char c=cin.get();
Example
#include<iostream>
using namespace std;
int main()
{
char c=cin.get(); �cout<<c<<endl;
return 0;
}
void put()
cout.put(variable / character);
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;
}
getline(char ,int size)
char x[30];
cin.getline(x,30);
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;
}
write(char , int n)
cout.write(x,2);
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;
}
cin
cin>>variable / character / String / ;
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;
}
cout�
Syntax:
cout<< variable / charcter / string;
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;
}
Formatted console input output operations�
width(n)
cout<<setw(int n);
Example
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x=10;
cout<<setw(20)<<x;
return 0;
}
fill(char)
cout<<setfill('character')<<variable;
Example
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x=10;
cout<<setw(20);
cout<<setfill('#')<<x;
return 0;
}
precison(n)
cout<<setprecision('int n')<<variable;
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float x=10.12345;
cout<<setprecision(5)<<x;
return 0;
}
Working with Files
Working with files generally requires the following kinds of data communication methodologies:
File Handling Classes
Operations in File Handling
�
Opening & Closing a file in C++
Opening a file
Files can be opened in two ways. They are:
- Useful when we use only one file.
- Manage multiple files using one stream.
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:
void close();
Reading from and writing to file
Opening file using constructor
ofstream outfile(“result”);
outfile<<<“total”;
ifstream infile(“data”);
infile>>number;
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();
}
Opening file using open() function
fstream stream_obj;
stream_obj.open(“file_name”);
Example
ofstream outfile;
outfile.open(“data1”);
--------------
---------------
outfile.close();
outfile.open(“data2”);
--------------
---------------
outfile.close();
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();
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();
}
Detecting end-of-file
More about open file modes
object.open(“filename”,mode);