Solved

Read pascal dat files without record information

Posted on 2004-08-30
11
723 Views
Last Modified: 2010-08-05
Hi all,

Can anybody help me - I have some old pascal dat files that I wish to access but have lost the information of the records.

Can this be done?
0
Comment
Question by:dbdp
  • 4
  • 2
  • 2
  • +1
11 Comments
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11930817
Sure, it can be done! But with a certain level of "success". You'll have to figure out the record length, probaby by trial and error or by looking at a hex dump of part of the file. Once you know the record length, it's just a matter of determining what data you want out of the record, and where in the record it is stored.

Can't say much more without a better idea of what you want to do.
0
 

Author Comment

by:dbdp
ID: 11934363
Well like I said I have some several old dat files that were part of a flat file application I had created long ago- the application is long corrupted and source code lost - all my predecessor backed upo were the dat files which data I would like to transfer to a database proper!!
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11935235
So, to get started you might try to determine the record size. You could do this by dumping out the beginning of the file in hex, maybe with a sidebar of the ascii chars. Then you can look for the beginning of the same field (that you hopefully will recognize) in two adjacent records. Simple counting will give you the record size. You can then create a record:

rUnknown=record
  case integer of
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11935275
So, to get started you might try to determine the record size. You could do this by dumping out the beginning of the file in hex, maybe with a sidebar of the ascii chars. Then you can look for the beginning of the same field (that you hopefully will recognize) in two adjacent records. Simple counting will give you the record size. You can then create a record:

const
reclen=size you determined

type
rUnknown=record
  case integer of
    0:(data:array[1..reclen] of byte);
    1:(field1: ...
        field2: ...
      )


You fill in the field types as you determine with your detective work. I usually write a little problem to read and dump the fields of the first 5 or ten records...

var
  F:file of runknown;
  R:rUnknown;

begin
  assign(F,'filename');
  reset(F);

  for I:=1 to 5 do begin
    read(f,R);
   writeln(I,': field 1 = ',R.field1);
 end;

  close(F);
end;

Then, through a simple trial and error, I can determine each field.

I hope that helps!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:dbdp
ID: 11939674
It already helps a lot but having been a while since I touched pascal - could you explain further the rUnknown record a bit more?

I have determined that each record of one f the files has 6 "fields" but am not sure of what - sting - integer etc - how would I define the record?


0
 
LVL 7

Accepted Solution

by:
LRHGuy earned 250 total points
ID: 11941285
rUnknown is just an example record. I don't know what your database is about. For example, it could be

rRecipes=record
  Name...
end;

A record definition would look something like that/this:

type
rUnknown=record
  case integer of
    0:(data:array[1..reclen] of byte);
    1:(field1: string[12]
        field2: byte;
        field3: word;
      )

Fill in the 1: section with your fields as you discover them. The 0 section keeps the record length at whatever you discovered the record length to be.

Say after you've got the hex dump, you discover the record length is 30 bytes:

const
  reclen=30;

Hopefully, you can then determine the fields. (with out a sample hex dump, this is difficult to demonstrate.) You may see that the first byte looks somewhat random, yet is followed by characters. This could be a string. The first byte may be the length of the string (not the field, necessarily).. So you examine the records, and decide to try 10 as the first field string length:
type

rUnknown=record
  case integer of
    0:(data:array[1..reclen] of byte);
    1:(field1: string[10]
        field2: byte;
        field3: word;
      )

Then you try it, running the program I showed above, to dump out the first few records:

  for I:=1 to 5 do begin
    read(f,R);
   writeln(I,': field 1 = ',R.field1);
 end;

A visual inspection may show that 10 for the length is too small...make changes and try again. Work your way field by field through the record.

Hope that helps!
0
 

Expert Comment

by:GaryStanton414
ID: 11944686
Hi. LRHGuy is correct. If you get a hex editor e.g. http://www.handshake.de/user/chmaas/delphi/download/xvi32.zip like this...open your dat file. You can imediately pick out strings which look like this for example:

♫GaryStanton

This may also be followed by a number of those boxes...any boxes after the text which are still part of the string will contain zeros on the left hand side view (hex view) in the hex editor (it is possible that they could also be representing the number 0 rather than empty space in the string). If you know what the 6 fields are this helps a lot. Example: if you know that one field is name this will of course be a string, if one field is height (in feet) then the sensible datatype to assume is byte. You can check the values of different datatypes a differnet locations in the file by using the data inspector. To do this click the tools menu, then options...click the data inspector tab and make sure that short,byte,word,integer and longint are selected...click ok...then click the tools menu and select data inspector...a small window will pop up saying data inspector...you can then click at a location on the hex view (the one with all the numbers on) and values will appear in the data inspector for each data type.

So where you think there is a number field click there and check which data type gives the most reasonable value for what the field is. E.G if you know the field is "height" (in feet) and the data inspector shows a reasonable value (e.g 6) in the inspector for a byte then it is likely to be a byte datatype. If several datatypes look reasonable then move on to the next field. E.g if shoe size was the next field and you moved along one from height...if 0 came up for byte in data inspector this is not a reasonable shoe size...you move along again and 6 comes up for a byte...this is reasonable...this means that height must have taken up 2 spaces (2 bytes) which is either a word,or an integer. Since you cant have a negative height integer is out of the question, so it must have been a word.

I hope this helps you furtur.
0
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 250 total points
ID: 12049889
> any boxes after the text which are still part of the string will contain zeros on the left hand side view (hex view) in the hex editor

This doesn't have to be true, though. Technically, it is undefined. If the same string variable has been changed and written to disk, the second string might end with part of the first string, if the second string happens to be shorter than the second one...

Still, a hex editor can be useful to detect the layout of the file.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12272167
No objections here.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Hyena v12.2 is now available for downloading and is available in English, French, German and Spanish versions.
Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now