C++ : File I/O Part 2

1. Explain the use of File I/O.

2. Open file using constructor and method.

3. Finding the End of File.

4. Fstream class & different Modes:

1. ios::in

2. ios::out

3. ios::app

4. ios::ate

5. Example showing the use of reading & writing data into the File.


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
C++ File I/O - Part 2.
*/

#include
#include

using namespace std;

int main()
{
char rel[20];

ofstream fout;
fout.open("one.txt");

fout<<"First Line \n";
fout<<"Second Line \n";
fout<<"Third Line \n";
fout<<"Fourth Line \n";
fout<<"Fifth Line\n";

fout.close();

//Reading all values using while(fin)

ifstream fin("one.txt");

while(fin)
{
fin.getline(rel,20);
cout<<"\n "<<rel;
}

fin.close();

//Reading all values using eof() method

fin.open("one.txt");

while(1)
{
if(fin.eof()!=0)
{
break;
}
fin.getline(rel,20);
cout<<"\n "<<rel;
}

fin.close();

//fstream Class object + Mode

fstream file("one.txt",ios::in);

while(1)
{
if(file.eof()!=0)
{
break;
}
file.getline(rel,20);
cout<<"\n "<<rel;
}

file.close();

return 0;
}

Advertisements

Leave a comment

Filed under C++, File I/O

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s