TEXT FILES

        Non binary files are known as text files. They do not include the ios::binary flag in their opening mode. These files are designed to store text. All values that we input or output are likely to suffer some formatting transformations, which do not necessarily correspond to their literal binary value. Data output operations on text files are performed in the same way as we work with cout. Similarly data input from text file can be performed in the same way as we do with cin.

get() and put() functions

The ifstream member function get() reads a character from a specified file. The ofstream member function put() writes a character into a specified output stream.

Program:         textfile.cpp

//This program illustrates opening a text file and writing few lines into it.

#include<iostream.h>
#include<fstream.h>
void main()
{
    ofstream mfile(“example.txt”);  //opening file for output
    if(mfile.is_open())
    {
             mfle<<”this is the first line./n”;  //writing on the file
             mfile<<”this isthe second line./n”;
             mfile.close();        //closing the file
    }
else
       cout<<”unable to open the file”;
}

Program:           textfile0.cpp

//This program illustrates reading a text from keyboard and writing it on a specified file
#include<iostream>
#include<fstream>
#include<iomanip.h>
#define max 2000
void  main()
{
        Ofstream mfile;          //defining the file object for input
        char  fname[10],line[max];
        cout<<”enter the name of file to be opened .\n”;
        cin>>fname;
        mfile.open(fname);
         cout<<”enter text and terminate with @\n”;
         cin.get(line ,max,’@’);
        cout<<”get input is:\n”;
        cout<<line;
        cout<<”storing onto file:”<<fname<<”\n”;
        mfile<<line;                             //writing on the line
        mfile.close();                         //closing the file
}

Program      text file1.cpp

#include<iostream>
#include<fstream>
#include<string.h>
void main()
{
       string line;
       ifstream mfile(“example.txt”);    //opening file for input
      if(mfile.is_open())
{
        while(!mfile.eof())
        {
           getline(mfile,line);      //reading th contents of file
           cout<<line<<endl;       //displaying the contents
       }
mfile.close();                  //closing the file
}
else
           cout<<”unable to open the file”;
}
Program             textfile2.cpp

//This program illustrates reading a text file character by character and
// display the contents on the screen.

#include<iostream>
#include<stdlib.h>
void main()
{
   ifstream  mfile;                 //opening a file for input
    char  fname[10],ch;
     cout<<”enter the name of file to be opened”;
     cin>>fname;
     mfile.open(fname);             //opening the file
    if(mfile.fail())
    {
        cout<<”no such file exists\n”;
exit(1);
}
while(!mfile.eof())
{
      ch=(char)mfile.get();         //reading the contents of file
      cout.put(ch);             //displaying the content
}
mfile.close();                //closing a file

}
Posted by Unknown On 03:20 No comments

0 comments:

Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Blog Archive

Contact Us


Name

E-mail *

Message *