-
please help
i have an assignment about file handling.
i am saving data to file in binary format using following statement
a.getdata();
fpt = fopen("stock.dat", "ab");
fwrite( &a, sizeof(a), 1, fpt );
fclose(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 );
a.putdata();
}
fclose(fpt);
break;
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.
thnx.
-
It's shocking me how often this one is coming up.
Look here;
http://www.cprogramming.com/cboard/s...threadid=18036
And at some of the other EOF threads in this forum.
[edit]
Sorry, there are a number of EOF threads in the C forum at the moment, not this one.
[/edit]
-
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:
ofstream
seek
write
( 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.
Later,
-
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
like this
fpt.open("stock.dat", ios:: binary | ios :: ate | ios::out );
cout << "record no";
cin >> rec;
loc = (rec-1)*sizeof(a);
if ( fpt.eof() )
{
fpt.clear();
}
fpt.seep(loc);
a.getdata() // nre data
fpt.write( ( char * )&a, sizeof a );
fpt.close();
but the respected recored is not updated
please show me the way