Docstoc

File Handling In C Language

Document Sample
File Handling In C   Language Powered By Docstoc
					File Handling Concepts
       using C++

     Prof. Salman Naseer Adil
Outline
          •   Introduction
          •   Data Hierarchy
          •   Streams & Types of Streams
          •   Basic File Operations
          •   Formatted I/O
          •   Character & Line I/O
          •   Binary I/O
          •   Writing & Reading Objects
          •   Random Access Files

                                           2
  Introduction
Introduction      Why We Need File Handling?
Data Hierarchy
Streams & Types     • All programs so far, input/output was temporarily
of Streams
Basic File
                          o Input through keyboard
Operations                o Output received on monitor
Formatted I/O       • If there is an mistake, all data will have to be entered again!
Character &
Line I/O
                    • This method is suitable when input & output is small.
Binary I/O
Writing &           • But not suitable when input & output require data in large quantity
Reading Objects
Random Access
Files               • Storage of Data
                        o Arrays, variables are temporary
                        o Files are permanent
                              Magnetic disk, optical disk, tapes
  Data Hierarchy
Introduction
Data Hierarchy    • Bit – The smallest item in memory, two stable states i.e. 1 or 0
Streams & Types   • Character – Letters (A~Z, a ~ z), Digits (0~9), Special symbols ($^&*#@) etc…
of Streams
Basic File        • Field – Group of characters conveying some meaning
Operations                  e.g. name of a person
Formatted I/O     • Record – Combination of related fields, often used in DBMS
Character &
Line I/O
                            e.g. Name, phone number, address – are referred as three fields
Binary I/O        • File – File are composed with related records or information
Writing &                   e.g. Record of BS ( IT ) 5th Semester in PUGC [ FILE ]
Reading Objects                                    Sally    Black
Random Access                                      Tom      Blue
Files                                                                                   File
                                                   Judy     Green
                                                   Iris     Orange
                                                   Randy    Red


                                          Judy   Green                         Record
                                      Judy          Field
                                     01001010       Byte (ASCII character J)

                                      1    Bit
  Streams
Introduction      Streams
Data Hierarchy      • In C++ file are referred as sequence of bytes which ends with End-Of-
Streams & Types       File (EOF) marker.
of Streams
Basic File                0   1   2   3   4   5   6   7   8    9         ...   n-1
Operations                                                               ...         end-of-file marker
Formatted I/O
Character &
Line I/O            • Streams are flow of data from source to destination.
Binary I/O          • In C++, Special classes known as streams used while working with
Writing &             files.
Reading Objects
Random Access       • The objects of streams classes are associated with files.
Files               • Then, the objects of streams classes are used to control the flow of data.
                    • Here is stream hierarchy
                                                                   ios


                                                  istream                      ostream


                                          ifstream            iostream               ofstream


                                                              fstream
  Types Of Streams
Introduction      Types of Streams
Data Hierarchy
Streams & Types     • Commonly used C++ stream classes:
of Streams
Basic File
Operations              o ifstream
Formatted I/O               It is the name of a C++ class also known as input stream & is used
Character &                  while working with file.
Line I/O                    Used to perform input operations on data files stored on disk.
Binary I/O
Writing &                   Objects of this stream reads data from files to main memory.
                             Hard Disk (Files)           C++ Program                Main Memory
Reading Objects
Random Access           o ofstream
Files
                            It is also a C++ class known as output stream.
                            Used to perform output operations on data files stored on disk.
                            Objects of this stream store data from main memory to files.

                        o fstream
                             It is also known as I/O stream.
                             Objects of this class performs both operations i.e. I/O
  Opening files & Modes
Introduction      Opening Files
Data Hierarchy      • It is required to include fstream header file in the program to perform IO
Streams & Types       operations otherwise compiler will generate a compiling error.
of Streams          • A file must be opened with appropriate mode before reading or writing data to it.
Basic File
Operations
                          • Syntax – stream objectName(“file name”,file opening mode);
Formatted I/O       • The following C++ program opens a file, namely, newFile.txt with output mode.
Character &              #include <iostream>
                         #include <fstream>
Line I/O
                         int main()
Binary I/O
                         {
Writing &                   // opening file using constructor
Reading Objects            ofstream temp("newFile.txt", ios::out);
Random Access              return 0;
Files                    }
                    • The mode specifies the operation to be performed on files.
                        o Input operation – Flow of data from file (Hard disk) to C++ program (main memory)
                        o Output operation – Flow of data from C++ program (main memory) to file (Hard
                           disk)
                  Default Modes
                    • Member function open() in stream classes uses a default mode
                         o ofstream default mode is ios::out
                         o ifstream default mode ios::in
                         o fstream default mode ios::in | ios::out
  File Operation Modes
Introduction      • According to situation – A data file can have more than one mode.
Data Hierarchy    • Following are different file operation modes and their uses:
Streams & Types        • ofstream myFile(“myFile.txt”, ios::out | ios::app );
of Streams        • Above code segment opens a file in output mode to add data at the end of the myFile.txt
Basic File             Mode           Descriptions
Operations
Formatted I/O                         Opens a file in output mode to write data into the file. If the file with same name
Character &       ios::out            already exists in default directory, all data in the file will be deleted & the file will be
                                      open as a new file.
Line I/O
Binary I/O                            Opens a file in output mode to append (add) data at the end of the file. If the file
Writing &         ios::app            already exist, it will be opened without deleting any data. If the file doesn't exist on
Reading Objects                       the disk, a new file will be created.
Random Access     ios::ate            Moves the file pointer to end of the file when the file is opened.
Files
                  ios::in
                                      Opens file in input mode to input data from the file. Data can only be read from the
                                      file in input mode.
                  ios::trunc          The data of the file is deleted if the file exists on the disk.

                  ios::nocreate
                                      No new file is created while opening a file. It means if the file doesn't not exists the
                                      open operation will fails!

                  ios::noreplace
                                      Don’t replace of the file exist already. If the file with the same name exists the
                                      opens operation will fails

                  ios::binary
                                      Opens a file in binary format. Int this mode data is stored and accessed in binary
                                      format.
  Verify & Closing files
Introduction      Testing File Open Operation
Data Hierarchy      • Member function is_open(), takes no arguments, returns true or false.
Streams & Types     • Previous program with Testing File Open Operation
of Streams
Basic File              #include <iostream>
Operations              #include <fstream>
Formatted I/O           int main()
Character &             {
Line I/O                  ofstream afile("newFile.txt", ios::out);
Binary I/O                if(afile.is_open())                // file is opened
                                      cout <<" The file is opened successfully.";
Writing &
                          else                               // error in opening file.
Reading Objects
                                      cerr << "Error in opening file.";
Random Access             return 0;
Files                   }
                    • cerr object is usually used to print error messages to console.
                  Closing Files
                    • A file must be closed for future use when I/O operations are finished.
                    • To close a file, the member function close() is used with the help of dot
                      operator. The following code segment shows this:
                         ofstream afile(“newfile.txt”, ios::out);
                           // performing operations i.e. your work etc…
                         afile.close();     // member function helps to close file.
  Formatted I/O Files
Introduction      Formatted I/O Files
Data Hierarchy      • In this format data is stored in files as series of characters. Each of 1 byte (8bits)
Streams & Types     • For example the number 38.125 is stored as 6 bytes ‘3’,’8’,’.’,’1’,’2’,’5’
of Streams          • Not efficient way to store data as it can take large amount of space for simple
Basic File
Operations
                      values
Formatted I/O       • Easy to implement & understand.
Character &
Line I/O          I/O Operations
Binary I/O
                    • The insertion operator << is used with cout object to send output on screen
Writing &
Reading Objects     • In the same way – same operator is used with the object created by ofstream
Random Access         or fstream class to send data to files
Files                    • Syntax – object << obj ; // obj can be variable, structure, objects

                    • Same case applies when reading data from files
                    • The extraction operator >> is used with the object of ifstream or fstream
                      class to read data from files.
                         • Syntax – object >> obj ; // obj can be variable, structure, objects
  Writing Data to Formatted I/O Files
                  #include <iostream.h>
Introduction      #include <conio.h>
Data Hierarchy    #include <fstream.h>
Streams & Types   int main()
of Streams        {
Basic File                 clrscr();
Operations                 int n = 100;
Formatted I/O              char ch = '*';
Character &                double d = 38.125;
Line I/O                   ofstream toFile("test1.txt"); // default mode is ios::out
Binary I/O                 if(toFile.is_open())
                           {
Writing &
                                    // file is opened
Reading Objects
                                    toFile << n <<endl;
Random Access                       toFile << ch <<endl;
Files                               toFile << d <<endl;
                                    cout<<"Data has been written in the file."<<endl;
                                    toFile.close();
                           }
                           else
                           {
                                    cerr << "Error: file opening error." <<endl;
                           }
                           return 0;
                  }
  Reading Data from Formatted I/O Files
                  #include <iostream.h>
Introduction      #include <conio.h>
Data Hierarchy    #include <fstream.h>
Streams & Types   int main()
of Streams        {
Basic File                 clrscr;
Operations                 int n;
Formatted I/O              char ch;
Character &                double d;
Line I/O                   ifstream fromFile("test1.txt"); // default mode is ios::in
Binary I/O                 if(fromFile.is_open())
                           {
Writing &
                                    // file is opened for input operation
Reading Objects
                                    fromFile >> n;
Random Access                       fromFile >> ch;
Files                               fromFile >> d;
                                    cout << "Read operation completes."<<endl;
                                    // output to console
                                    cout << n <<"\n"<<ch<<"\n"<<d<<endl;
                                    fromFile.close();
                           }
                           else
                                    cerr << "Error: file opening error." <<endl;
                           return 0;
                  }
  Detecting End-Of-File (EOF)
Introduction        • The eof() member function is used to check whether the control of execution
Data Hierarchy        has reach the end of file or not with the help of loop.
Streams & Types     • In this way – the programmer can go through the whole file.
of Streams               o Example – a file contains names of the students.
Basic File        #include <iostream.h>
Operations        #include <conio.h>
                  #include <fstream.h>
Formatted I/O     int main()
Character &       {
Line I/O                    clrscr();
Binary I/O                  char* name;
                            int count = 0; // to count the names - counter
Writing &                   ifstream fromFile("students.txt"); // default mode is ios::in
Reading Objects             if(fromFile.is_open())
Random Access               {
Files                                 // file is opened for input operation
                                      while(!fromFile.eof())
                                      {
                                                fromFile >> name;    // input from file
                                                cout << name <<endl; // output to console.
                                                count++;
                                      }
                                      cout <<"---------\nTotal names: "<<count<<endl;
                                      fromFile.close();   // I/O finished – close file.
                            }
                            else
                                      cerr << "Error: file opening error." <<endl;
                            return 0;
                  }
  Writing Data as Series of Characters
Introduction          • With the help of member function put() single character can be written.
Data Hierarchy        • Example – following program inputs five characters and store them in test4.txt file
Streams & Types   #include <iostream.h>
of Streams        #include <conio.h>
Basic File        #include <fstream.h>
Operations        int main()
                  {
Formatted I/O               clrscr();
Character &                 char ch;
Line I/O                    ofstream toFile("test4.txt"); // default mode ios::out
Binary I/O                  if(toFile.is_open())
                            {
Writing &                             // file is opened
Reading Objects                       for(int i = 1; i <= 5 ; i++)
Random Access                         {
Files                                           cout<<"Enter a character: ";
                                                cin>>ch;
                                                        // toFile object is associated with test4.txt file
                                                        toFile.put(ch);
                                           }
                                           cout<<"\nFive character has been written to file."<<endl;
                                           toFile.close();
                               }
                               else
                                           cerr << "Error: file opening error." <<endl;
                               return 0;
                  }
  Reading Data as Series of Characters
Introduction           With the help of member function get(char) single character can be read from file.
Data Hierarchy         Example – following program reads five characters and output them on console
                       #include <iostream.h>
Streams & Types        #include <conio.h>
of Streams             #include <fstream.h>
Basic File             int main()
Operations             {
                                 clrscr();
Formatted I/O                    char ch;
Character &
Line I/O                         ofstream toFile("test4.txt"); // default mode ios::out
Binary I/O                       if(toFile.is_open())
                                 {
Writing &                                  // file is opened
Reading Objects                            for(int i = 1; i <= 5 ; i++)
Random Access                              {
Files                                                cout<<"Enter a character: ";
                                                     cin>>ch;
                                                       // toFile object is associated with test4.txt file
                                                       toFile.put(ch);
 TRY YOURSELF!                               }
                                             cout<<"\nFive character has been written to file."<<endl;
 Write a program                             toFile.close();
  with the help of               }
  get() function, it             else
 should count the                            cerr << "Error: file opening error." <<endl;
 number of vowels                return 0;
      in a file.       }
  Reading Line by Line
Introduction      • The string stored with insertion operator << should not contains whitespaces
Data Hierarchy      otherwise text after whitespace will be ignored.
Streams & Types   • To solve this problem we use line approach with the help of member function
of Streams          getline() of ifstream or ofstream.
Basic File             o Syntax – object.getline(char*,int length, char);
Operations
Formatted I/O     o Following program reads data as line by line and output to console.
                  #include <iostream.h>
Character &       #include <fstream.h>
Line I/O          int main()
Binary I/O        {
                     char input_line[80];
Writing &            ifstream twinkle("Twinkle.txt", ios::in);
Reading Objects      if(twinkle.is_open())
Random Access        {
Files                     while(!twinkle.eof())
                          {
                                    twinkle.getline(input_line, 80, '\n');
                                    cout << input_line <<endl;
                          }
                          twinkle.close();
                     }
                     else
                     {
                          cerr << "File opening Error." << endl;
                     }               C++ Program
                     cout << endl;
                     return 0;
                  }
  Binary I/O
Introduction
Data Hierarchy        • Data or records can be stored and retrieve in / from files in binary format.
Streams & Types       • It is an efficient way to store large amount of data as compared to formatted I/O.
of Streams            • For example – Each integer in binary format takes two bytes
Basic File            • The integer 30752 will take only two bytes to be stored in the file while in
Operations
Formatted I/O           formatted I/O this value takes 5 bytes to store.
Character &           • Binary format is commonly used.
Line I/O
Binary I/O          Opening File in Binary Mode
Writing & Reading
Objects
                      • Simple method – while opening file just mention the binary mode i.e.
Random Access             • Ofstream afile(“afile.txt”, ios::binary);
Files
                    Closing Files in Binary Mode
                           • no difference – binary files are close in the same way as the formatted files
                             are closed.
                           • Object.close();
  Writing & Reading in Binary Mode
Introduction
Data Hierarchy      • Writing & Reading in binary format / mode is slightly different.
Streams & Types     • To write data to files in binary format the member function write() of
of Streams            ofstream or fstream class is used.
Basic File          • Syntax – object.write((char*)&obj, sizeof(obj));
Operations
Formatted I/O            object          Name of object of ofstream or fstream stream (class).
Character &
Line I/O                 write           Member function of stream used to write data in binary mode.
Binary I/O               char*           Address of character data in fixed length bytes.
Writing & Reading
Objects                  obj             Name of variable that contains data to be written.
Random Access
Files
                         sizeof(obj)     Calculates the size of obj in bytes to store data.

                    • To read data from files in binary format the member function read() of
                      ifstream or fstream class is used.
                    • Syntax - object.read((char*)obj, sizeof(obj));
  Writing in Binary Mode
Introduction
                    This program will prompt for five integers and then stores them in a file using
Data Hierarchy      binary mode.
Streams & Types
of Streams          #include <iostream.h>
                    #include <fstream.h>
Basic File
                    #include <stdlib.h> // exit();
Operations
                    int main()
Formatted I/O       {
Character &           ofstream toFile("data.txt", ios::out | ios::binary);
Line I/O              if(!toFile)
Binary I/O            {
Writing & Reading         cout<<"File opening error.";
Objects                   exit(1);
Random Access         }
Files                 int var;
                      for( int i = 1; i<= 5; i++)
                      {
                          cout << "Enter an Integer: ";
                          cin >> var;
                          toFile.write((char*)&var, sizeof(var));
                      }
                      toFile.close();
                      return 0;
                    }
  Reading in Binary Mode
Introduction
                    This program will read five integers from file in binary mode and then display
Data Hierarchy      them on console.
Streams & Types
of Streams          #include <iostream.h>
                    #include <fstream.h>
Basic File
                    #include <stdlib.h> // exit();
Operations
                    int main()
Formatted I/O       {
Character &           ifstream fromFile("data.txt", ios::in | ios::binary);
Line I/O              if(!fromFile)
Binary I/O            {
Writing & Reading         cout<<"File opening error.";
Objects                   exit(1);
Random Access         }
Files                 int var;
                      for( int i = 1; i<= 5; i++)
                      {
                          fromFile.read((char*)&var, sizeof(var));
                          cout<<var<<endl;
                      }
                      fromFile.close();
                      return 0;
                    }
  Writing a Structure Variable
Introduction        • Structure variables/objects can also be stored in files normally in binary
Data Hierarchy        mode.
Streams & Types     • To store a structure variable use write() member function of ofstream
of Streams
Basic File            or fstream with same procedure as describe earlier.
Operations          • Following code segment stores a structure variable to file in binary
Formatted I/O         mode.
Character &         Struct Employee
Line I/O            {
Binary I/O             char name[30], address[50];
Writing & Reading      int salary;
Objects             };
Random Access
Files               Ofstream toFile(“emp.txt”,ios::out | ios::binary);

                    Employee imran; // declaring an object of struct Employee

                    // input data in imran variable
                    toFile.write((char*)&imran, sizeof(imran));
                    toFile.close();


                    • In the same way – use read() member function of ifstream or fstream to
                      read data from files.
  Class Objects I/O
Introduction        • Objects created by classes can also be store/load from files usually in
Data Hierarchy        binary mode.
Streams & Types     • It helps the user to use as much memory as needed.
of Streams
Basic File          • Efficient way to store the state of contents of an object.
Operations
Formatted I/O       Writing an Object
Character &
Line I/O            • To write an object to file, the member function write() of ofstream or
Binary I/O            fstream class is used.
Writing & Reading   • Syntax to output / write an object to file:
Objects                    object.write((char*)&obj, sizeof(obj));
Random Access
Files
                    Reading an Object
                    • In the same way, to read an object from the file, the member function
                      read() of ifstream or fstream class is used.
                    • Syntax to input/read an object from file
                           object.read((char*)&obj, sizeof(obj));
  Writing into file
                    #include <iostream.h>
Introduction        #include <fstream.h>
Data Hierarchy      #include <conio.h>
                    #include <stdlib.h>
Streams & Types     class Record
of Streams          {
Basic File                    private:
Operations                              char code[5], name[15], address[20];
                                        int pay;
Formatted I/O                 public:
Character &                             void inputData() // inline member function
Line I/O                                {
Binary I/O                                        cout <<"\nEnter Code: ";
                                                  cin >> code;
Writing & Reading                                 cout <<"Enter Name: ";
Objects                                           cin >> name;
Random Access                                     cout <<"Enter Address: ";
Files                                             cin >> address;
                                                  cout <<"Enter Pay: ";
                                                  cin >> pay;
                                        }
                    };
                    void main()
                    {
                              Record record;
                              char op;
                              ofstream toFile("emp.dat", ios::out | ios::binary);
  Writing into file (cont.)
                        if(!toFile)
Introduction            {
Data Hierarchy                    cerr << "File opening error" <<endl;
                                  exit(1);
Streams & Types         }
of Streams              do
Basic File              {
Operations                         record.inputData();
                                  toFile.write((char*)&record, sizeof(record));
Formatted I/O                     cout <<"\nDate has been saved!"<<endl;
Character &                       cout <<“\nMore Records [y/n]? "; cin >> op;
Line I/O                } while(op == 'y' || op == 'Y');
Binary I/O              toFile.close();
                    }
Writing & Reading
Objects
Random Access
Files
  Reading from file
                    #include   <iostream.h>
Introduction        #include   <fstream.h>
                    #include   <conio.h>
Data Hierarchy      #include   <stdlib.h>
Streams & Types     #include   <iomanip.h>
of Streams
                    class Record
Basic File          {
Operations                     private:
Formatted I/O                                 char code[5], name[15], address[20];
                                              int pay;
Character &                     public:
Line I/O                                      void showData()
Binary I/O                                    {
                                                         cout<<code<<", "<<name<<", “
Writing & Reading                                                   <<address<<", "<<pay<<endl;
Objects                                       }
                    };
Random Access       void main()
Files               {
                               Record record;
                               ifstream fromFile("emp1.dat", ios::in | ios::binary);
                               if(!fromFile)
                               {
                                          cerr << "File opening error" <<endl; exit(1);
                               }
                               while(!fromFile.eof())
                               {
                                          fromFile.read((char*)&record, sizeof(record));
                                          record.showData();
                               }
                               fromFile.close();
                    }
  Random Access Files
Introduction        Random Access Mechanism
Data Hierarchy       • Another way to access records randomly is called Random Access, also known
Streams & Types        as instant access or quick access.
of Streams           • The required record or data can be accessed directly, if exists, without having
Basic File
Operations
                       access the preceding records.
Formatted I/O        • Records in binary I/O are stored with fixed length i.e. one integer takes only 2
Character &            bytes etc…
Line I/O             • Each record (object) takes same number of bytes in the memory.
Binary I/O           • So, it helps the user to move to a particular location in the file to access a
Writing & Reading
Objects
                       particular record.
Random Access
Files                • Suppose – an object can store marks (in Integer) of six subject of a student.
                     • So the memory required to store an object is (# of Subjects) * 2 byte = 12 bytes
                       per object
  File Pointers
Introduction        File Pointer
Data Hierarchy        • A file pointer is used to access the record or information in files.
Streams & Types       • Each file object (ifstream, ofstream or fstream) has two file pointers.
of Streams                  1.   get pointer – associated with ifstream
Basic File                       The get pointer indicates the byte number in the file from where the next input will
Operations                       occur.
Formatted I/O
Character &
                            2. put pointer – associated with ofstream
Line I/O
Binary I/O
                               The put pointer indicates the byte number in the file from where the output will be
Writing & Reading              made.
Objects                      Note: The fstream object have both get & put pointers.
Random Access
Files                 There are two member functions of streams to control the movement of these pointers:

                            1.     The seekg()
                                   Short for Seek get – it is associated with ifstream for input purpose .

                            1.     The seekp()
                                   Short for Seek put – it is associated with ofstream for output purpose.
  The seekg() function
                    • This function is used to specify the position of the file pointer for reading data from file.
Introduction
Data Hierarchy                   Syntax – object.seekg(position,                offset);
Streams & Types
of Streams          • Position [ Integer parameter ]
Basic File                Specify the byte number from where the data is to be read
Operations
Formatted I/O
Character &
                    • Offset [ Integer parameter ]
Line I/O                  Specify the position of file pointer for input / reading at the start of file.
Binary I/O                Starting value of offset is 0 which is also the first character in file & so on…
Writing & Reading         Positive Offset indicates to move forward in the file.
Objects                   Negative Offset means to move backward in the file.
Random Access             The possible value are:
Files                     o      ios::beg – Compiler will count the position from start of the file i.e. first character
                             (default value)
                          o      ios::cur – Compiler will count the position from its current position.
                          o      ios::end – Compiler will count the position from the end of the file.
  The seekg() function Example
                    #include <iostream.h>
Introduction        #include <conio.h>
                    #include <string.h>
Data Hierarchy      #include <fstream.h>
Streams & Types     int main()
of Streams          {
                               char sentence[80], ch;
Basic File                     fstream myFile("seek.txt", ios::out | ios::in | ios::binary);
Operations                     if(myFile.is_open())
Formatted I/O                  {
                                          strcpy(sentence,
Character &                               "Programming - To Control the Behavior of Computer!");
Line I/O                                  myFile << sentence;
Binary I/O                                cout <<sentence<<" - String Length:
                                                     ("<<strlen(sentence)<<")"<<endl; // total characters
Writing & Reading                         cout << "Executing seekg(25)"<<endl;
Objects                                   myFile.seekg(25); // moving file pointer 25 character forward
                                          cout << "The contents from position 25:";
Random Access                             while(!myFile.eof())
Files                                     {
                                                     myFile.get(ch); // onward from 25th character
                                                     if(!myFile.eof())
                                                                cout << ch;
                                          }
                                          cout<< endl << endl;
                                          myFile.close();
                               }
                               else
                                          cerr << "File Opening error." <<endl;

                              return 0;
                    }
  The seekp() function
                    • It is totally opposite of seekg() function
Introduction
Data Hierarchy
                    • This function is used to specify the position of the file pointer for output / storing data into file.
Streams & Types                  Syntax – object.seekp(position,                  offset);
of Streams
Basic File          • Position [ Integer parameter ]
Operations                Specify the byte number from where the data is to be written.
Formatted I/O
Character &
Line I/O            • Offset [ Integer parameter ]
Binary I/O                Specify the position of file pointer at the start of file.
Writing & Reading         Starting value of offset is 0 which is also the first character in file & so on…
Objects                   Positive Offset indicates to move forward in the file.
Random Access             Negative Offset means to move backward in the file.
Files                     The possible value are:
                          o      ios::beg – Compiler will count the position from start of the file i.e. first character
                             (default value)
                          o      ios::cur – Compiler will count the position from its current position.
                          o      ios::end – Compiler will count the position from the end of the file.




                                                   BEST OF LUCK!
Congratulations!




                   End of this
                    Chapter
       “One of my most productive days was throwing away 1000 lines of code”
                                (Ken Thompson)


                         “When in doubt, use brute force”
                                (Ken Thompson)

				
DOCUMENT INFO
Shared By:
Stats:
views:111
posted:4/4/2012
language:English
pages:31