CPP Language / Stream classes in CPP language

These are classes used for input and output operations on files and Input / output devices. iostream.h and fstream.h header file consists of all the stream classes.

Type Of Stream Functions Used
OUT STREAM COUT, PUTS
IN STREAM CIN, GETS


Examples programs on streams
Stream Functions Program Output
COUT

#include
using namespace std;
int main()
{
cout<<"Wisdom Materials";
}

Wisdom Materials
PUTS
#include
using namespace std;
int main()
{
puts("Wisdom Materials");
}

Wisdom Materials
CIN
#include
using namespace std;
int main()
{
string str;
cout<<"Enter a string:";
cin>>str;
cout<<"Number entered using cin is "< }


Enter a string:
Wisdom
Output
Wisdom
GETS
#include
using namespace std;
int main()
{
char str[10];
puts("Enter a String:");
gets(str);
puts("Entered a String: ");
puts(str);
}


Enter a String:
Wisdom
Entered a String:
Wisdom
Read and write data to a file
#include
#include
int main()
{
char data[200];
fstream file;
file.open ("data.txt", ios::out | ios::in );
cout << "Enter data to write to a file" << endl;
cin.getline(data, sizeof(data));

//Writing to file
file << data << endl;

//Reding from file
file >> data;
cout << data << endl;
file.close();
return 0;
}


Enter data to write to a file
Wisdom
Output
Wisdom


Home     Back