C++ Data File Handling

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

By: itsgurminder (15 month(s) ago)

thnx sir,very informatic ppt

Presentation Transcript

DESIGNED BY: : 

DESIGNED BY: Mr. Roop Narayan PGT(Computer Sc.) Kendriya Vidyalaya, NER, Izzat Nagar, Bareilly Contact No.: 9411882746 Email: roop_alwar@yahoo.co.in

Slide 2: 

WORKING WITH FILES Definition: A file is an area on the disk . It is a stream, which is a collection of bits. Types of file stream: There are two types of File stream. Input file stream: That supplies data to the programs from the files. Output file stream: That supplies data to the files.

Slide 3: 

CLASSES FOR FILE STREAM OPERATIONS : ifstream: It provide input operations. Contains open() with default input mode. It inherits the functions get(), getline(), read(), seekg() and tellg() from istream. ofstream: It provide output operations. Contains open() with default output mode. It inherits the functions put(), seekp(), tellp() and write() from ofstream. fstream: It support for simultaneously input and output operations. Contains open() with default input mode. It inherits all the functions from iostream.

Slide 4: 

OPENING A FILE: A file can be opened by using any of the following two ways: Using the constructor function of the class Using the member function open() of the class

Slide 5: 

(1) OPENING A FILE USING CONSTRUCTOR: As we know a constructor is used to initialize an object while it is being created. Same as we can open a file by supplying the name of the file to the constructor of the file stream as follows: Example: ofstream obj(“abc.dat”); ifstream file(“STORY.TXT”); fstream fileio(“xyz.txt”);

Slide 6: 

(2) OPENING A FILE USING OPEN() As we know the function open() is the member function in all the file streams ( ifstream,ofstream & fstream). We can open a file of a particular type of file stream with its default mode or by providing the other modes explicitly, as follows: Example: ifstream obj; obj.open(“STORY.TXT”); ofstream file; file.open(“product.dat”); fstream abc; abc.open(“xyz.dat”,ios::binary|ios::io|ios::out);

Slide 7: 

CLOSING A FILE: A file is closed using the member function close(), as follows: Example: ifstream obj; ----------- ----------- obj.close();

Slide 8: 

DETECTING END-OF-FILE: Detecting end of file condition is necessary for preventing any further attempt to read data from the file. An end of a file can be detected by using any of the following two ways. while(obj)//obj is the object of a file stream. An ifstream object returns a value 0 if any error occur in the file operation including end of file condition. Thus the while loop terminates when obj returns a value zero on reaching at the end of file. Remember this loop may terminate due to other failures as well.

Slide 9: 

if(fin1.eof()!=0) {exit(1);} // inside a infinite loop. or while(!obj.eof()) The function eof() is a member function of ios class. It returns a non zero value if the end of file(EOF) condition is encountered otherwise return zero.

Slide 10: 

FILE MODES: The file mode parameter in open() and constructor function specifies the purpose for which the file is opened. If we don’t supply the mode parameter when we open a file it use the default values in the absence of the actual value. The default values are as follows: ios::in for ifstream functions meaning open for reading only. ios::out for ofstream functions meaning open for writing only.

Slide 11: 

OTHER FILE MODES & THEIR MEANING: Parameter Meaning ios::app - Append to end of file. ios::ate - Go to end of file on opening ios:binary - Binary file ios::in - open file for reading only ios:nocreate - open fails if the file does not exist ios::noreplace - open fails if the file already exists ios::out - open file for writing only ios::trunc - delete the contents of the file if it exists

Slide 12: 

FILE POINTERS AND THEIR MANIPULATIONS: Each file has two associated pointers known as the file pointers. One of them is called the input pointer or get pointer and the other is called the output pointer or put pointer. We can use these pointers to move through the files while reading or writing. When we open a file for reading only, the input pointer is automatically set at the beginning so that we can read the file from the start. When we open a file in writing mode only, the existing contents are deleted and the output pointer is set at the beginning. This enable us to write to the file from the start. When we open a file for append to add more data, this moves the output pointer to the end of the file.

Slide 13: 

FUNCTIONS FOR MANIPULATION OF FILE POINTERS: The file stream classes support the following functions to move a file pointer to any other desired position inside the file. seekg(): Moves get pointer (input) to a specified location. seekp(): Moves put pointer (output) to a specified location. tellg(): Gives the current position of the get pointer. tellp(): Gives the current position of the put pointer. The seekg & tellg functions are associated with get pointer and seekp & tellp functions are associated with put pointer.

Slide 14: 

NOTES REGARDING SEEK FUNCTIONS: seekg() and tellg() can also be used with two arguments as follwos: seekg(offset, refposition): seekp(offset, refposition); The parameter offset represents the number of bytes the file pointer is to be moved from the location specified by the parameter refposition. The refposition takes one of the following three constants defined in the ios class: ios::beg start of the file ios::cur current position of the pointer ios::end end of the file The seekg() function moves the associated file’s get pointer while the seekp() function moves the associated file’s put pointer.

Slide 15: 

POINTER OFFSET CALLS : Seek call Action fout.seekg(0,ios::beg); - go to start fout.seekg(0,ios::cur); - stay at the current position fout.seekg(0,ios::end); - go to end of file fout.seekg(m,ios::beg); - move to (m+1)th byte in the file. fout.seekg(m,ios::cur); - go forward by m byte from the current position fout.seekg(-m,ios::cur); - go forward by m bytes from the current position fout.seekg(-m,ios::cur); - go backward by m bytes from the end

Slide 16: 

SEQUENTIAL INPUT AND OUTPUT OPERATIONS: The file stream classes support a number of member functions for performing the input and output operations on files. There are two pair of functions for this purpose- put() & get() functions : these functions are designed for handling a single character at a time. syntax: obj.put(ch); obj.get(ch); 2. write() & read() functions: these functions are designed to write and read blocks of binary data. syntax: obj.write((char*)&var, sizeof(var)); obj.read((char*)&var, sizeof(var)); Here var is an variable of either class or structure.

Slide 17: 

UPDATING A FILE: It is a routine task in the maintenance of any data file. The updation include one or more of the following tasks: Displaying the contents of a file. Modifying an existing item. Adding a new item. Deleting an existing item.