Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with editing FILE

Status
Not open for further replies.

darr01

Programmer
Oct 24, 2001
28
0
0
MY
Hi, guys. I have another problem which have been trying to solve without any solution for more than a month.

I am doing a simple database program using the struct data structure. I managed to do add, delete, search and display the records entered without any problems but when it comes to edit, sometimes (and sometimes only!) I have problem after writing the edited record back to database.

I use fread to read each record and if there is a match, there will be a menu to ask which data in a record needed to be edited.

However, using fwrite to saved back the edited file, my edited record have some bytes missing from the record.

e.g.
Book Title: ed Dragon (which is suppose to be Red Dragon)
Book Author: homas Harris (suppose to be Thomas harris)

You get the picture. Seemed to me that, the position of the pointer is off by 1 byte.

Does anyone have this problem before? Could it be a compiler problem? I am using Visual C++ Professional on a Windows XP Pro machine.

Sorry for not providing the codes because it is very long.

Thanks in advance,

Darryl
 
Additional information that I gathered for this problem:

I use ftell to check the position of the pointers while adding each record. The problem lies in the addition of the first record. Sometimes (this occurs at random) when I add the first record to a new file (txt file), the position of the pointer is 201 whereas my record size (struct data structure) is only 200 bytes! Subsequent records added will be saved at positions 401, 601, 801 and so on. The problem is, when I save an edited record back to the file, it interprets the record size correctly and save the record at 200, 400, 600 and so on. That's why the edited record is off by 1 byte.

Any solution or explanation to this problem?

Thanks,

Darryl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top