FILE HANDLING IN       C++MELJUN CORTES, BSCS,ACS
Files (Streams) Files are used to store data in a relatively permanent form, on floppy disk, hard disk, tape or other form...
Main memory                   Secondary memory• Made up of RAM chips.      • Usually a disk drive (or• Used to hold a prog...
C++ STREAMSA Stream is a general name given to flow of data.Different streams are used to represent different kinds of d...
Flow of Data….                                    Data                       PROGRAM      Input                           ...
The following classes in C++ haveaccess to file input and outputfunctions:           ifstream           ofstream          ...
The Stream Class Hierarchy                          NOTE : UPWARD ARROWS INDICATE                                   THE BA...
DIFFERENT FILE OPERATIONS OPENING A FILE CLOSING A FILE READING FROM A FILE WRITING ON A FILE CHECKING FOR END OF FILE
OPENING A FILE(Associating a stream with a file)1. By using the CONSTRUCTOR of the            stream class.  ifstream tran...
File Mode ParametersPARAMETER          MEANING  Ios::app         Append to end-of file  Ios::ate         goto end of file ...
Checking For Successful File Opening ifstream transaction(“sales.dly”); if (transcation == NULL) {      cout<<“unable to o...
Closing of File Stream_name.close(); e.g., transaction.close();
Types of Files . The two basic types are – text and – binary. A text file consists of readable characters separated into l...
A binary file stores data to disk in the sameform in which it is represented in mainmemory.  If you ever try to edit a bin...
When using a binary file we write wholerecord data to the file at once. When using atext file, we write out separately eac...
For the binary file we will use write towrite to the file, whereas for the text file wewill use the usual output operator(...
EXAMPLES   Creation of a text file
:    Types of File AccessSequential access. With this type of fileaccess one must read the data inorder, much like with a ...
FILE POINTERS
FILE POINTERS Each file object has two integer values associated with it : – get pointer – put pointer These values specif...
File pointers….. By default reading pointer is set at the beginning and writing pointer is set at the end (when you open f...
Functions associated with filepointers : The seekg() and tellg() functions allow you to set and examine the get pointer. T...
seekg() function : With one argument :  seekg(k) where k is absolute position from the beginning. The start of the file is...
seekg() function :With two arguments :   the first argument represents an offset from a particular   location in the file....
seekg() function :With two arguments : Begin                                         End                ^                 ...
//                                                      //clrscr();                                                       ...
MELJUN CORTES File handler C Lecture
Upcoming SlideShare
Loading in...5
×

MELJUN CORTES File handler C Lecture

968

Published on

MELJUN CORTES File handler C Lecture

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
968
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
51
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

MELJUN CORTES File handler C Lecture

  1. 1. FILE HANDLING IN C++MELJUN CORTES, BSCS,ACS
  2. 2. Files (Streams) 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. The following is a comparison of the two types of storage:
  3. 3. Main memory Secondary memory• Made up of RAM chips. • Usually a disk drive (or• Used to hold a program magnetic tape). when it is running, • Used to hold files (where a file including the values of its can contain data, a program, variables (whether text, etc.) integer, char, an array, • Can hold rather large amounts etc.) of data.• Can only hold relatively • Is fairly permanent. (A file small amounts of data. remains even if the power goes• Is temporary (as soon as out. It will last until you erase it, the program is done or as long as the disk isnt the power goes out all of damaged, at least.) these values are gone). • Access to the data is• Gives fast access to the considerably slower (due to data (all electronic). moving parts).
  4. 4. C++ STREAMSA Stream is a general name given to flow of data.Different streams are used to represent different kinds of data flow.Each stream is associated with a particular class, which contains member functions and definitions for dealing with that particular kind of data flow.
  5. 5. Flow of Data…. Data PROGRAM Input Output Stream Stream >> DEVICES OR << (Extraction (Insertion operator) FILES operator) Dataistream class ostream class
  6. 6. The following classes in C++ haveaccess to file input and outputfunctions: ifstream ofstream fstream
  7. 7. The Stream Class Hierarchy NOTE : UPWARD ARROWS INDICATE THE BASE CLASS iosistream ostream fstreambaseget() put()getline() write()read() iostream <<>>Ifstream Ofstream fstreamOpen() Open()Tellg() Tellp()Seekg() Seekp()
  8. 8. DIFFERENT FILE OPERATIONS OPENING A FILE CLOSING A FILE READING FROM A FILE WRITING ON A FILE CHECKING FOR END OF FILE
  9. 9. OPENING A FILE(Associating a stream with a file)1. By using the CONSTRUCTOR of the stream class. ifstream transaction(“sales.dly”); ofstream result(“result.02”);2. By using the open() function of the stream class ifstream transaction; transaction.open(“sales.dly”);
  10. 10. File Mode ParametersPARAMETER MEANING Ios::app Append to end-of file Ios::ate goto end of file on opening Ios::binary binary file Ios::in Open existing file for reading Ios::nocreate open fails if file doesn’t exist Ios::noreplace open fails if file already exists Ios::out creates new file for writing on Ios::trunc Deletes contents if it existsThe mode can combine two or more modes using bit wiseor ( | )
  11. 11. Checking For Successful File Opening ifstream transaction(“sales.dly”); if (transcation == NULL) { cout<<“unable to open sales.dly”; cin.get(); // waits for the operator to press any key exit(1); }
  12. 12. Closing of File Stream_name.close(); e.g., transaction.close();
  13. 13. Types of Files . The two basic types are – text and – binary. A text file consists of readable characters separated into lines by newline characters. (On most PCs, the newline character is actually represented by the two-character sequence of carriage return (ASCII 13), line feed (ASCII 10).
  14. 14. A binary file stores data to disk in the sameform in which it is represented in mainmemory. If you ever try to edit a binary filecontaining numbers you will see that thenumbers appear as nonsense characters. Nothaving to translate numbers into a readableform makes binary files somewhat moreefficient.Binary files also do not normally useanything to separate the data into lines. Sucha file is just a stream of data with nothing inparticular to separate components.
  15. 15. When using a binary file we write wholerecord data to the file at once. When using atext file, we write out separately each of thepieces of data about a given record.The text file will be readable by an editor,but the numbers in the binary file will notbe readable in this way.The programs to create the data files willdiffer in how they open the file and in howthey write to the file.
  16. 16. For the binary file we will use write towrite to the file, whereas for the text file wewill use the usual output operator(<<) andwill output each of the pieces of the recordseparately.With the binary file we will use the readfunction to read a whole record, but withthe text file we will read each of the piecesof record from the file separately, using theusual input operator(>>)
  17. 17. EXAMPLES Creation of a text file
  18. 18. : Types of File AccessSequential access. With this type of fileaccess one must read the data inorder, much like with a tape, whetherthe data is really stored on tape or not.Random access (or direct access). Thistype of file access lets you jump to anylocation in the file, then to any other,etc., all in a reasonable amount oftime.
  19. 19. FILE POINTERS
  20. 20. FILE POINTERS Each file object has two integer values associated with it : – get pointer – put pointer These values specify the byte number in the file where reading or writing will take place.
  21. 21. File pointers….. By default reading pointer is set at the beginning and writing pointer is set at the end (when you open file in ios::app mode) 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.
  22. 22. Functions associated with filepointers : The seekg() and tellg() functions allow you to set and examine the get pointer. The seekp() and tellp() functions allow you to set and examine the put pointer.
  23. 23. seekg() function : With one argument : seekg(k) where k is absolute position from the beginning. The start of the file is byte 0 File End Begin k bytes ^ File pointer The seekg() function with one argument
  24. 24. seekg() function :With two arguments : the first argument represents an offset from a particular location in the file. the second specifies the location from which the offset is measured. Begin End ^ Offset from Begin The seekg() function with two argument
  25. 25. seekg() function :With two arguments : Begin End ^ Offset from Begin ^ Offset from end ^ Offset from current position The seekg() function with two argument
  26. 26. // //clrscr(); getch();#include <fstream.h> cout <<"reading from created file n";#include <conio.h> infl.open("try.txt");#include <stdio.h> out.open("cod.dat");void main() //**********************************{ c=infl.get(); //clrscr(); do char c,d,ans; { d=c+1; char str[80]; cout<<c<<d<<n; ofstream outfl("try.txt"),out("cod.dat"); out.put(d); c= infl.get(); ifstream infl; } do while (c!=0); { cout<<"please give the string : "; out<<0; gets(str); infl.close(); outfl<<str; outfl.close(); getch(); cout <<"do you want to write more...<y/n> : "; //********************************* ans=getch(); } } while(ans==y); outfl<<0; outfl.close();
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×