Binary file writing corrupted
Posted on 2001-08-30
I have a small program that stores information to a binary file that uses the following record as its format:
type Tdata = string;
problem = record
item1 : string;
item2 : string;
item3 : string;
info : Array [0..9] of Tdata;
numItems : integer;
The program allows the user to either open an existing binary file to edit/update, or to create a new file to store entries in.
The array of Tdata called info is used to store string data of length > 255. When the user inputs data into a memo box, the string is broken down into 255-character chunks and placed in info[x].
My problem is that if a user creates a new file and then writes data out to it, the binary file will have data in it from the file that was open before the user created the new file if the following condition occurs:
The previously opened file had text of length > 255 stored in the info array, thus meaning more than info has data in it.
When the user creates a new file, the program closes the previous file if one is open and clears out all of the data structures and variables holding data at the time, so the new file starts off with a completely blank slate. I have stepped through the program multiple times watching every variable that stores data on the records in the files, and they are all cleared out properly before the new file is even opened. The odd thing is that if the user uses the program to view the record, they will not see the data from the old file. The program only reads in the data that it is supposed to, but if I open up the binary file in notepad, I can see the text there from the previous file.
This may not be clear at all, but I hope you see what I am getting at. I just want to know where that unwanted text from the previous file is coming from, and how to get rid of it.