help me...damn crappy c++ project

targetblimp

Disciple
hey guys

i am trying to make a menu driven programme on handling mediclaim policy using classes and functions.
the programme should have 4 options-adding records ,modifying and deleting them using file handling.:huh:
someone just tell me how to use classes with file handling and i'll be glad

:thanx:
 
An object of a class simply stores it's data linearly starting from a single point in memory. If you wish to store a certain object (of size x bytes) of a class in a file, you just need to copy x bytes of data starting from a particular pointer. For this you can use the write function. For example -

fout.write( (char *) &obj, sizeof(obj) );

Where obj is the object you wish to write. The char * is required to convert the pointer to the object to a character pointer, which the write() function can accept. Similiarly you can use the read() function to read an object from a file.
 
^^ Raw writing the data to a file wont help. If he is to open the file manually then it would make no sense. U have to use some delimiter.

@targetblimp. : Just proceed with a single option first like adding records to a file. Then do rest of the things later. And specify EXACTLY what u need. Do u need the whole program ??
 
But does he really need to access the file manually? If not, then writing raw data should be ok, as the program can read it in quite the same way. But if manual access is desired, you are right, and I guess each data of the object would need to be added seperately.
 
If you use a delimiter then 'getline' function should be very helpful. It reads from a file until a user specified delimiter is encountered. Moreover it also moves the current file pointer to that location.
 
To know the best of a programming type.

Just always try to utilise the Help Index in ur IDE of ur Compiler.

U can always get atleast a single clue.

As a book I can suggest u H. Schildt.

I hope u will get wat u need.
 
ujjwal said:
An object of a class simply stores it's data linearly starting from a single point in memory. If you wish to store a certain object (of size x bytes) of a class in a file, you just need to copy x bytes of data starting from a particular pointer. For this you can use the write function. For example -

fout.write( (char *) &obj, sizeof(obj) );

Where obj is the object you wish to write. The char * is required to convert the pointer to the object to a character pointer, which the write() function can accept. Similiarly you can use the read() function to read an object from a file.

Thats the worst thing to do. if you have a class with virtual functions, you will be copying the vptr table address also to the file and when you read it back, it can lead to a disaster.

The right way to do it is to either put read and write methods inside the class or else overload the insertion and extraction (<< and >>) operators so that the object can be serialized and you can use the stream classes to write to or read from the file.
 
now, i can write data to the file. but i really have no idea on how to search for a specific record and how to modify it.

the program must prompt which record you want to modify?

then it must ask which entry to modify.
also i was thinking of searching records by entries. for e.g-

someone wants to modify any record.the program will ask- do you remember the record no.
if he doesn't remember he can search for the record by entering his name he had earlier stored inthe record.after thats done he must enter a password only then can he modify the records.
 
Back
Top