Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
FILE HANDLING IN C++
SYLLABUS OUTLINE <ul><li>Data File Handling:  </li></ul><ul><ul><li>Need for a data file,  </li></ul></ul><ul><ul><li>Type...
<ul><li>Header file :  </li></ul><ul><ul><li>fstream.h; ifstream, ofstream, fstream classes;  </li></ul></ul><ul><ul><li>O...
<ul><li>All programs we looked earlier:  </li></ul>Introduction  <ul><li>input data from the keyboard . </li></ul><ul><li>...
Files (Streams) <ul><li>Files are used to store data in a relatively permanent form, on floppy disk, hard disk, tape or ot...
Why use files? <ul><li>Convenient way to deal with large quantities of data.  </li></ul><ul><li>Store data permanently (un...
<ul><li>The following is a  </li></ul><ul><li>comparison  </li></ul><ul><li>of  </li></ul><ul><li>the two types of storage...
<ul><ul><ul><li>Main memory   </li></ul></ul></ul><ul><ul><ul><li>Made up of RAM chips.  </li></ul></ul></ul><ul><ul><ul><...
<ul><ul><ul><li>Main memory   </li></ul></ul></ul><ul><ul><ul><li>Can only hold relatively  small  amounts of data.  </li>...
I/O in C++ <ul><li>I/O in C++ uses  streams </li></ul><ul><li>A Stream is a general name given to  </li></ul><ul><li>flow ...
Flow of Data…. PROGRAM DEVICES OR FILES Input Stream >> Output Stream << Data Data istream class ostream class (Insertion ...
More About Files….. <ul><li>Now we need to know: </li></ul><ul><ul><li>how to &quot; connect &quot;  file  to  program </l...
I/O in C++ <ul><li>Different streams are used to represent different kinds of  data flow. </li></ul><ul><li>Each stream is...
The following classes in C++ have access to file input and output functions: <ul><li>ifstream </li></ul><ul><li>ofstream <...
The Stream Class Hierarchy ios istream get()‏ getline()‏ read()‏ >> ostream put()‏ write()‏ << fstreambase iostream Ifstre...
OPENING A FILE <ul><li>1. By using the  CONSTRUCTOR of the    stream class. </li></ul><ul><li>ifstream transaction(“sales....
File Mode Parameters <ul><li>PARAMETER MEANING </li></ul><ul><li>ios::app Append to end-of file </li></ul><ul><li>ios::ate...
Checking For Successful File Opening <ul><li>ifstream transaction(“sales.dly”); </li></ul><ul><li>if (transcation == NULL)...
Closing of File <ul><li>Stream_name.close(); </li></ul><ul><li>e.g., transaction.close(); </li></ul><ul><li>fl.close(); </...
Types of Files <ul><li>The two basic types of files are </li></ul><ul><ul><li>Text files </li></ul></ul><ul><ul><li>&  </l...
Text Files <ul><li>A text file consists of  readable  characters separated into lines by newline characters.  </li></ul><u...
<ul><li>A  binary file  stores data to disk in the same form in which it is represented in main memory. </li></ul><ul><li>...
<ul><li>Not having to translate numbers into a readable form makes binary files somewhat more  efficient . </li></ul><ul><...
<ul><li>When using a  binary  file we write whole record data to the file at once.  </li></ul><ul><li>but the numbers in t...
<ul><li>for the  text file  we will use the usual  output operator(<<)  and will output each of the pieces of the record s...
: Sequential access . With this type of file access one must read the data in order, much like with a tape, whether the da...
FILE POINTERS
FILE POINTERS <ul><li>Each file object has two integer values associated with it : </li></ul><ul><ul><li>get pointer </li>...
File pointers….. <ul><li>By default  reading pointer  is set at  the  beginning . </li></ul><ul><li>By default  writing po...
Functions associated with file pointers : <ul><li>The  seekg()  and  tellg()  functions allow you to set and examine the  ...
seekg() function :  (with one argument) <ul><li>With one argument : </li></ul><ul><li>fl.seekg(k); </li></ul><ul><li>fl.se...
‘ seek’ functions : ( With two arguments ) Number of bytes  file  pointer to be moved Location   from where File  pointer ...
File Pointer offset calls <ul><li>fl.seekg(0,ios::beg); Go to start </li></ul><ul><li>fl.seekg(0,ios::cur); Stay at the cu...
File Pointer offset calls <ul><li>fl.seekg(m,ios::cur);   Go forward by m bytes  </li></ul><ul><li>from current pos </li><...
seekg() function : ( With two arguments ) : Go backward by m bytes from the end m bytes fl.seekg(m,ios::cur); Go forward b...
EXAMPLES <ul><li>Creation of  a  text  file </li></ul>
<ul><li>#include <fstream.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>v...
The End
Upcoming SlideShare
Loading in …5
×

File Handling In C++

89,846 views

Published on

Published in: Technology

File Handling In C++

  1. 1. FILE HANDLING IN C++
  2. 2. SYLLABUS OUTLINE <ul><li>Data File Handling: </li></ul><ul><ul><li>Need for a data file, </li></ul></ul><ul><ul><li>Types of data files – Text file and Binary file; </li></ul></ul><ul><li>Text File : </li></ul><ul><ul><li>Basic file operations on text file: </li></ul></ul><ul><ul><ul><li>Creating/Writing text into file, </li></ul></ul></ul><ul><ul><ul><li>Reading and </li></ul></ul></ul><ul><ul><ul><li>manipulation of text from an already existing text File (accessing sequentially); </li></ul></ul></ul><ul><li>Binary File : </li></ul><ul><ul><li>Creation of file, </li></ul></ul><ul><ul><li>Writing data into file, </li></ul></ul><ul><ul><li>Searching for required data from file, </li></ul></ul><ul><ul><li>Appending data to a file, </li></ul></ul><ul><ul><li>Insertion of data in sorted file, </li></ul></ul><ul><ul><li>Deletion of data from file, </li></ul></ul><ul><ul><li>Modification of data in a file; </li></ul></ul><ul><li>Implementation of above mentioned data file handling in C++; Components of C++ to be used with file handling: </li></ul>
  3. 3. <ul><li>Header file : </li></ul><ul><ul><li>fstream.h; ifstream, ofstream, fstream classes; </li></ul></ul><ul><ul><li>Opening a text file in in, out, and app modes; </li></ul></ul><ul><ul><li>open(), </li></ul></ul><ul><ul><li>get(), </li></ul></ul><ul><ul><li>put(), </li></ul></ul><ul><ul><li>getline() and </li></ul></ul><ul><ul><li>close() functions; </li></ul></ul><ul><ul><li>Detecting end-of-file (with or without using eof() function); </li></ul></ul><ul><ul><li>Opening a binary file using in, out, and app modes; </li></ul></ul><ul><ul><li>open(), read(), write() and close() functions; </li></ul></ul><ul><ul><li>Detecting end-of-file (with or without using eof() function); </li></ul></ul><ul><ul><li>tellg(), tellp(), seekg(), seekp() functions </li></ul></ul>SYLLABUS OUTLINE (Contd.)
  4. 4. <ul><li>All programs we looked earlier: </li></ul>Introduction <ul><li>input data from the keyboard . </li></ul><ul><li>output data to the screen. </li></ul><ul><li>Difficult to handle large amount of input data. </li></ul><ul><li>Output would also be lost as soon as we exit </li></ul><ul><li>from the program. </li></ul><ul><li>How do we store data permanently?. </li></ul><ul><li>We can use secondary storage device. </li></ul><ul><li>Data is packaged up on the storage device as data structures called files . </li></ul>
  5. 5. Files (Streams) <ul><li>Files are used to store data in a relatively permanent form, on floppy disk, hard disk, tape or other form of secondary storage. Files can hold huge amounts of data if need be. Ordinary variables (even records and arrays) are kept in main memory which is temporary and rather limited in size. </li></ul>Lets put it in points…………..
  6. 6. Why use files? <ul><li>Convenient way to deal with large quantities of data. </li></ul><ul><li>Store data permanently (until file is deleted). </li></ul><ul><li>Avoid having to type data into program multiple times. </li></ul><ul><li>Share data between programs. </li></ul>
  7. 7. <ul><li>The following is a </li></ul><ul><li>comparison </li></ul><ul><li>of </li></ul><ul><li>the two types of storage……….. </li></ul>
  8. 8. <ul><ul><ul><li>Main memory </li></ul></ul></ul><ul><ul><ul><li>Made up of RAM chips. </li></ul></ul></ul><ul><ul><ul><li>Used to hold a program when it is running, including the values of its variables (whether integer, char, an array, etc.) </li></ul></ul></ul><ul><ul><ul><li>Secondary memory </li></ul></ul></ul><ul><ul><ul><li>Usually a disk drive (or magnetic tape). </li></ul></ul></ul><ul><ul><ul><li>Used to hold files (where a file can contain data, a program, text, etc.) </li></ul></ul></ul><ul><ul><ul><li>Can hold rather large amounts of data. </li></ul></ul></ul>
  9. 9. <ul><ul><ul><li>Main memory </li></ul></ul></ul><ul><ul><ul><li>Can only hold relatively small amounts of data. </li></ul></ul></ul><ul><ul><ul><li>Is temporary (as soon as the program is done or the power goes out all of these values are gone). </li></ul></ul></ul><ul><ul><ul><li>Gives fast access to the data (all electronic ). </li></ul></ul></ul><ul><ul><ul><li>Secondary memory </li></ul></ul></ul><ul><ul><ul><li>Can hold rather large amounts of data. </li></ul></ul></ul><ul><ul><ul><li>Is fairly permanent . (A file remains even if the power goes out. It will last until you erase it, as long as the disk isn't damaged, at least.) </li></ul></ul></ul><ul><ul><ul><li>Access to the data is considerably slower (due to moving parts). </li></ul></ul></ul>
  10. 10. I/O in C++ <ul><li>I/O in C++ uses streams </li></ul><ul><li>A Stream is a general name given to </li></ul><ul><li>flow of data. </li></ul>
  11. 11. Flow of Data…. PROGRAM DEVICES OR FILES Input Stream >> Output Stream << Data Data istream class ostream class (Insertion operator)‏ (Extraction operator)‏
  12. 12. More About Files….. <ul><li>Now we need to know: </li></ul><ul><ul><li>how to &quot; connect &quot; file to program </li></ul></ul><ul><ul><li>how to tell the program to read data </li></ul></ul><ul><ul><li>how to tell the program to write data </li></ul></ul><ul><ul><li>error checking and handling eof </li></ul></ul>
  13. 13. I/O in C++ <ul><li>Different streams are used to represent different kinds of data flow. </li></ul><ul><li>Each stream is associated with a particular class , which contains </li></ul><ul><ul><li>member functions and </li></ul></ul><ul><ul><li>definitions for dealing with that particular kind of data flow. </li></ul></ul>
  14. 14. The following classes in C++ have access to file input and output functions: <ul><li>ifstream </li></ul><ul><li>ofstream </li></ul><ul><li>fstream </li></ul>File Related Classes
  15. 15. The Stream Class Hierarchy ios istream get()‏ getline()‏ read()‏ >> ostream put()‏ write()‏ << fstreambase iostream Ifstream Open()‏ Tellg()‏ Seekg()‏ Ofstream Open()‏ Tellp()‏ Seekp()‏ fstream NOTE : UPWARD ARROWS INDICATE THE BASE CLASS
  16. 16. OPENING A FILE <ul><li>1. By using the CONSTRUCTOR of the stream class. </li></ul><ul><li>ifstream transaction(“sales.dly”); </li></ul><ul><li>ofstream result(“result.02”); </li></ul><ul><li>2. By using the open() function of the stream class </li></ul><ul><li>ifstream transaction; </li></ul><ul><li>transaction.open(“sales.dly”); </li></ul>(Associating a stream with a file)‏
  17. 17. File Mode Parameters <ul><li>PARAMETER MEANING </li></ul><ul><li>ios::app Append to end-of file </li></ul><ul><li>ios::ate goto end of file on opening </li></ul><ul><li>ios::binary binary file </li></ul><ul><li>ios::in Open existing file for reading </li></ul><ul><li>ios::nocreate open fails if file doesn’t exist </li></ul><ul><li>ios::noreplace open fails if file already exists </li></ul><ul><li>ios::out creates new file for writing on </li></ul><ul><li>ios::trunc Deletes contents if it exists </li></ul>The mode can combine two or more modes using bit wise or ( | )
  18. 18. Checking For Successful File Opening <ul><li>ifstream transaction(“sales.dly”); </li></ul><ul><li>if (transcation == NULL)‏ </li></ul><ul><li>{ </li></ul><ul><li>cout<<“unable to open sales.dly”; </li></ul><ul><li>cin.get(); // waits for the operator to press any key </li></ul><ul><li>exit(1); </li></ul><ul><li>} </li></ul>
  19. 19. Closing of File <ul><li>Stream_name.close(); </li></ul><ul><li>e.g., transaction.close(); </li></ul><ul><li>fl.close(); </li></ul><ul><li>Note : There is no need to give the physical file name at the time of closing a file. </li></ul>
  20. 20. Types of Files <ul><li>The two basic types of files are </li></ul><ul><ul><li>Text files </li></ul></ul><ul><ul><li>& </li></ul></ul><ul><ul><li>Binary files </li></ul></ul>
  21. 21. Text Files <ul><li>A text file consists of readable characters separated into lines by newline characters. </li></ul><ul><li>(On most PCs, the newline character is actually represented by the two-character sequence of carriage return (ASCII 13), line feed (ASCII 10). ( ) </li></ul>
  22. 22. <ul><li>A binary file stores data to disk in the same form in which it is represented in main memory. </li></ul><ul><li>If you ever try to edit a binary file containing numbers you will see that the numbers appear as nonsense characters. </li></ul>Binary Files
  23. 23. <ul><li>Not having to translate numbers into a readable form makes binary files somewhat more efficient . </li></ul><ul><li>Binary files also do not normally use anything to separate the data into lines. </li></ul><ul><li>Such a file is just a stream of data with nothing in particular to separate components. </li></ul>Binary Files
  24. 24. <ul><li>When using a binary file we write whole record data to the file at once. </li></ul><ul><li>but the numbers in the binary file will not be readable in this way. </li></ul><ul><li>When using a text file, we write out separately each of the pieces of data about a given record. </li></ul><ul><li>The text file will be readable by an editor </li></ul>Text Files Binary Files
  25. 25. <ul><li>for the text file we will use the usual output operator(<<) and will output each of the pieces of the record separately. </li></ul><ul><li>with the text file we will read each of the pieces of record from the file separately, using the usual input operator(>>) </li></ul><ul><li>For the binary file we will use write to write to the file, </li></ul><ul><li>With the binary file we will use the read function to read a whole record, </li></ul>The programs to create the data files will differ in how they open the file and in how they write to the file.
  26. 26. : Sequential access . With this type of file access one must read the data in order, much like with a tape, whether the data is really stored on tape or not. Random access (or direct access ). This type of file access lets you jump to any location in the file, then to any other, etc., all in a reasonable amount of time. <ul><li>Types of File Access </li></ul>
  27. 27. FILE POINTERS
  28. 28. FILE POINTERS <ul><li>Each file object has two integer values associated with it : </li></ul><ul><ul><li>get pointer </li></ul></ul><ul><ul><li>put pointer </li></ul></ul><ul><li>These values specify the byte number in the file where reading or writing will take place. </li></ul>
  29. 29. File pointers….. <ul><li>By default reading pointer is set at the beginning . </li></ul><ul><li>By default writing pointer is set at the end (when you open file in ios::app mode)‏ </li></ul><ul><li>There are times when you must take control of the file pointers yourself so that you can read from and write to an arbitrary location in the file. </li></ul>
  30. 30. Functions associated with file pointers : <ul><li>The seekg() and tellg() functions allow you to set and examine the get pointer . </li></ul><ul><li>The seekp() and tellp() functions allow you to set and examine the put pointer . </li></ul>
  31. 31. seekg() function : (with one argument) <ul><li>With one argument : </li></ul><ul><li>fl.seekg(k); </li></ul><ul><li>fl.seekp(k); </li></ul><ul><li>where k is absolute position from the beginning. The start of the file is byte 0 </li></ul><ul><li>It will result in moving the pointer as shown- </li></ul>Begin File End k bytes ^ File pointer
  32. 32. ‘ seek’ functions : ( With two arguments ) Number of bytes file pointer to be moved Location from where File pointer is to be moved fl.seekg(offset, refposition); fl.seekp(offset, refposition); Refposition takes one of the following forms : <ul><ul><li>ios::beg Start of the file </li></ul></ul><ul><ul><li>ios::cur current position of the pointer </li></ul></ul><ul><ul><li>ios::end End of the file </li></ul></ul>
  33. 33. File Pointer offset calls <ul><li>fl.seekg(0,ios::beg); Go to start </li></ul><ul><li>fl.seekg(0,ios::cur); Stay at the current position </li></ul><ul><li>fl.seekg(0,ios::end); Go to the end of file </li></ul><ul><li>fl.seekg(m,ios::beg); Move to (m+1)th byte in the file </li></ul>
  34. 34. File Pointer offset calls <ul><li>fl.seekg(m,ios::cur); Go forward by m bytes </li></ul><ul><li>from current pos </li></ul><ul><li>fl.seekg(-m,ios::beg); Go backward by m bytes </li></ul><ul><li>from current pos </li></ul><ul><li>fl.seekg(-m,ios::cur); Go backward by m bytes </li></ul><ul><li>from the end </li></ul>
  35. 35. seekg() function : ( With two arguments ) : Go backward by m bytes from the end m bytes fl.seekg(m,ios::cur); Go forward by m bytes from current pos <ul><li>fl.seekg(m,ios::beg); </li></ul>Move to (m+1)th byte in the file m bytes Begin End End Begin m bytes Begin End ^ Offset from Begin ^ Offset from current position ^ Offset from end fl.seekg(-m,ios::cur);
  36. 36. EXAMPLES <ul><li>Creation of a text file </li></ul>
  37. 37. <ul><li>#include <fstream.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>void main()‏ </li></ul><ul><li>{ clrscr(); </li></ul><ul><li>char c,d,ans; </li></ul><ul><li>char str[80]; </li></ul><ul><li>ofstream outfl(&quot;try.txt&quot;); </li></ul><ul><li>do </li></ul><ul><li>{ cout<<&quot;please give the string : &quot;; </li></ul><ul><li>gets(str); </li></ul><ul><li>outfl<<str; </li></ul><ul><li>cout <<&quot;do you want to write more...<y/n> : &quot;; </li></ul><ul><li>ans=getch(); </li></ul><ul><li>} while(ans=='y'); </li></ul><ul><li>outfl<<''; </li></ul><ul><li>outfl.close(); </li></ul>clrscr(); ifstream infl; getch(); cout <<&quot;reading from created file &quot;; infl.open(&quot;try.txt&quot;); out.open(&quot;cod.dat&quot;); //********************************** c=infl.get(); do { d=c+1; cout<<c<<d<<' '; out.put(d); c= infl.get(); } while (c!=''); out<<''; infl.close(); outfl.close(); getch(); //********************************* } Program to generate coded file…… (Text File)
  38. 38. The End

×