C++ TokensOperatorsExpressionsOperator overloadingControl statementsC++ functionsClass and objectsConstructor and destructorsOperator OverloadingType conversionsInheritanceAbstractionPolymorphismpointersInput/outputFile HandlingTemplatesException handling |
C++ updating a Fileupdating is a routine task in the maintenance of any data file. The updating include one or more of the following task in the below program we see how class objects can be written to and read from the disk files and length of the object is obtained with the help of sizeof operator. This length represent the sum total of length of all data members of the object
1. displaying The contents of a file. These actions require the file pointers to move to a exact location that corresponds to the object/item under consideration. This can be very easily implemented if the file contains a collection of items. objects of equal lengths, in such cases, the size of each object can be obtained using the below statement
Them the location of a desired object, say the mth object, may be obtained as follows
The location gives the byte number of the first byte of the m object. so now we can set the file pointer to reach this byte with the help of seekg() and seekp() we can also find the total number of objects in a file using object_length as follows
the file_size can be obtained using the function tellg() or tellg() or tellp() when the file pointer is located at the end of the file In the below program The program uses the STOCK.DAT file created using in the belor five items and perform the following operations on the file
1. Adds a new item to the file.
In the above program we are using the fstream class to declare the file streams. The fstream class inherits two buffers, one for input and another for output, and synchronize the movement of the file pointers on these buffers, that is , whenever we read from or write to the file, both the pointers on these buffers. That is whenver we read from or write to the file, both the pointer move in tanden, therefore at any point. therefore at any point of the time both the pointers point to the same bytes since we have to add new objects to the file as well as modify some of the existing objects, we open the file using ios::ate option for input and output operations. Remember the option ios:app allows us to add data to end of the file only. The ios::ate mode sets the file pointers at the end of the file when opening it. we must therefore at any point of time both the pointers point to the same byte At the end of reading the current contents of the file, the program sets the EOF flag on. this prevents any further reading from or writing to the file. The EOF flag is returned off by using the function clear(), which allows access to the file once again and the after appending a new item, the program display the contents of the appended file and also the total number of objects in the file and the memory space occupied by them To modify an object, we should reach to the first byte of that object. This is achieved using the statements
The program accepts the number and the new values of the object to be modified and updates it, finally, The concepts of the appended and modified file are displayed Remember we are opening an existing file for reading and updating the values. it is therefore essential that the data members are of the same type and declared in the same order as in the existing so the member function are not stored, they can be different |