C++ updating a File

C++ updating a File

updating 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.
2. Modifying an existing item.
3. Adding a new item.
4. Deleting an existing item

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

int object_length=sizeof(object);

Them the location of a desired object, say the mth object, may be obtained as follows

int location=m* object_length;

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

int n=file_size/object_length;

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.
2. Modifies the details of an item.
3. Displays the contents of the file.

FILE UPDATING:: RANDOM ACCESS
#include <iostream>
#include <fstream>
#include < iomanip >
class INVENTORY
{
char name[10];
int code;

float cost;
public :
void getdata()
{
std::cout << "Enter name:";
std::cin >> name;
std::cout << "Enter code";
std::cin >> code;
std::cout << "Enter cost";
std::cin >> cost;
}
void putdata()
{
std::cout << std::setw(10) << name;
std::cout << std::setw(10) << code;
std::cout << std::setprecision(2);
std::cout << std::setw(10) << cost;
}
}; // End the Class definition
int main()
{
INVENTORY item;
std::fstream inoutfile; // input and output stream
inoutfile.open("STOCK.DAT",std::iostream::ate| std::iostream::in|std::iostream::out|std::iostream::binary);
inoutfile.seekg(0,std::iostream::beg); // go to start
std::cout << "CURRENT CoNTENTS OF STOCK" << "\n";
while (inoutfile.read((char *)& item,sizeof item))
{
item.putdata();
}
inoutfile.clear();
/* >>>>>>>>> add one more item <<<<<<<<<<*/
std::cout << "\n ADD An Item";
item.getdata();
char ch;
std::cin.get(ch);
inoutfile.write((char *)& item,sizeof item); // display the appended file
inoutfile.seekg(0);
std::cout << "Contents of Appended File \n";
while (inoutfile.read((char *) & item,sizeof item))
{
item.putdata();
}
//Find number of object in the fileint last=inoutfile.tellg();
int last=inoutfile.tellg();
int n=last/sizeof(item);
std::cout<<"Number of objects="; std::cout << "Total bytes in the file=" << last;
/*>>>>>>>>>>>>>>>>>>> Modify The details of an item <<<<<<<<<<*/
std::cout << "Enter object number to be updated \n";
int object;
std::cin >> object;
std::cin.get(ch);
int location=(object-1)*sizeof(item);
if (inoutfile.eof())
inoutfile.clear();
inoutfile.seekp(location);
std::cout << "Enter new values of the object \n";
item.getdata();
std::cin.get(ch);
inoutfile.write((char *)& item, sizeof item)<< flushall; /*
>>>>>>>>>>>>>> SHOW UPDATE FILE <<<<<<<<<<<<<<<<<*/

inoutfile.seekg(0); // go to the start
std::cout << "CONTENTS OF UPDATE FILE \n";
while (inoutfile.read((char *)& item, sizeof item))
{
item.putdata();
}
inoutfile.close();
return 0;
} //

output:
CURRENT CONTENTS OF STOCK
AA 11 100
BB 22 200
CC 33 300
DD 44 400
XX 99 900
ADD AN Item
Enter name:YY
Enter code10
Enter cost101
Contents of Appended File
AA 11 100
BB 22 200
CC 33 300
DD 44 400
XX 99 900
yy 10 101
Number of objects=6
Total bytes in the file=96
Enter object number to be updated 6
Enter new values of the object
Enter name:ZZ
Enter code20
Enter cost201
Contents of Appended File
AA 11 100
BB 22 200
CC 33 300
DD 44 400
XX 99 900
ZZ 20 201

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

int ocation=(object-1)*sizeof(item);
inoutfile.seekp(location);

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

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise