Solved

Binary file writing corrupted

Posted on 2001-08-30
4
201 Views
Last Modified: 2010-04-06
I have a small program that stores information to a binary file that uses the following record as its format:

type Tdata = string[255];

type                                  
  problem = record
    item1 : string[3];              
    item2 : string[3];              
    item3 : string[3];              
    info : Array [0..9] of Tdata;      
    numItems : integer;                
  end;

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[0] 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.


chmod101
0
Comment
Question by:chmod101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Accepted Solution

by:
Pedja earned 100 total points
ID: 6442096
YOu are meeting usual behaviour of memory use. String contains information of length of data stored in it.
So when you use some string of lets saj 250 characters and they change it's value to string of two character, what happens is that only first three bytes of that string is actually changed. Delhi does not spend time to clear rest of memory space since it will not be used anyways.

But, when you save such record structure to file, all memory space used by structure will be saved.

This is normal behaviour. If you do not want this to happen, before setting new value to the string
previously set it to have value of 255 #0 or space characters. In that way, whole memory space for string will be filled with new value (cleared).

An example:

if you have string[4] variable a containing 'AAAA'
memory space it uses will look like this:

[04][41][41][41][41]

Then, if you set it's value to 'BB' memory will be like this:

[02][42][42][41][41]

what happened, zero byte contains information about number of characters in a string, first two bytes will contain new value, but last two will remain unchanged. However, application will "see" just first two bytes as part ov a string, as it is supposed so.

Now, if you set value to empty string, memory would look like this:

[00][42][42][41][41]

As you can see, change will be just in zero byte, length will be set to zero, but remainder of the memory space stays intact.

Hope this helps,
Pedja




0
 

Author Comment

by:chmod101
ID: 6464242
Pedja,

thank you for the comment, but as I said in the question, I am already clearing out all of the data structures before opening another file - that includes setting all of the string variables to null.

Any other thoughts?


chmod101
0
 

Expert Comment

by:Pedja
ID: 6465097
I explaned whz simply setting variable to null won't help. If zou set it to null nothing in the memorz changes bzt first byte stating that new string length iz 0. You have to fill in memory with #0's (or whatewer you want).

If you have

var
  a : string[3],

to clear it out you have to do the following:

a := #0#0#0;
a := '';

Then you will have clear memory.

This is necessary because writing record to field
does not store values but whole memory block
reserved for the record.

You may take a look at FillCHar() function in Delphi's help. I am not sure about right name, my help is somehow corrupted so I cannotopen it :( It fills memory block with specified character. That will allow you to fill record's memory space with #0 before storing values to it.


Pedja
0
 

Author Comment

by:chmod101
ID: 6470846
Thanks pedia,  my basic assumption was that when the debugger showed me that the string was empty, it really was empty.  Thanks for the help.

chmod101
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question