i have an assignment about file handling.
i am saving data to file in binary format using following statement
fpt = fopen("stock.dat", "ab");
fwrite( &a, sizeof(a), 1, fpt );
where a is object of class data
for printing file contents my code is
fpt = fopen("stock.dat", "r");
while ( feof(fpt) == 0 )
fread( &a, sizeof(a), 1, fpt );
now problem is here, at time of printing file contents is showing last record twice. What should i do ?
Also give me the idea to display the selected records using conditon given by user ( dbase style )
Code is also attached with this thread.
It's shocking me how often this one is coming up.
And at some of the other EOF threads in this forum.
Sorry, there are a number of EOF threads in the C forum at the moment, not this one.
Okay, correct me if I am wrong, as my C++ is not as strong as my C, though it appears as though you are using the C file access commands.
C++ has a differenct set of commands that should be used.
These commands are:
( Just to name a few )
I would suggest implementing the propper C++ programming instead of trying to merge C and C++ programming.
Anyway that is my 2 cents on the topic.
how to edit records...
i somehow managed to append records and to display the entire file contents which is stored in binary format.
Now i want to edit the particular record.
i tried it in following manner
1 . get record no from user
2 . calculate address at which the pointer must be located
3 . place pointer at desired location
4 . read new info from user
5 . update
fpt.open("stock.dat", ios:: binary | ios :: ate | ios::out );
cout << "record no";
cin >> rec;
loc = (rec-1)*sizeof(a);
if ( fpt.eof() )
a.getdata() // nre data
fpt.write( ( char * )&a, sizeof a );
but the respected recored is not updated
please show me the way