Recent

Author Topic: Strings in old typed binary files  (Read 792 times)

imperiusdamian

  • New Member
  • *
  • Posts: 14
Strings in old typed binary files
« on: April 10, 2023, 09:42:44 pm »
Good afternoon all,

I have a number of typed binary files created in a program written in an old version of Turbo Pascal (7.0). These files consist of records in the following format:

Code: Pascal  [Select][+][-]
  1. type
  2.    info = RECORD
  3.       string1, string2, string3, string4 : string;
  4.       int : integer;
  5.    end;

I am now working in FreePascal and I'm trying to read these old files and write them out to plain text files. Since FreePascal's strings can't be used in files I tried using ShortStrings and also string[255] - these are probably equivalent - but each time, the resulting text file is garbled with strange characters jumbled in with some of the data I know is in the old files.

The code I'm using is roughly this:

Code: Pascal  [Select][+][-]
  1. var
  2.    inforec : info;
  3.    oldfile : file of info;
  4.    newfile : text;
  5.    i, maxrecords : integer;
  6.  
  7. begin
  8.    assign(oldfile, 'oldfilename');
  9.    reset(oldfile);
  10.    assign(newfile, 'newfilename.txt');
  11.    rewrite(newfile);
  12.    maxrecords := filesize(oldfile);
  13.  
  14.    for(i := 1 to maxrecords) do
  15.    begin
  16.       read(oldfile, inforec);
  17.       writeln(newfile, inforec.string1);
  18.       writeln(newfile, inforec.string2);
  19.       writeln(newfile, inforec.string3);
  20.       writeln(newfile, inforec.string4);
  21.       writeln(newfile, inforec.int);
  22.    end;
  23.    close(newfile);
  24. end.

I'm probably missing something really obvious but whether I use ShortStrings or string[255] in my record definition I get the same garbled results in my text file (I know the files are not damaged because I can still read them using the old program on a DOS VM).

Does anyone have any suggestions as to what I might be doing wrong?

EDIT: Realised I posted this in the wrong section. Mods can you please move this topic to the FreePascal General board? Thanks!  :D
« Last Edit: April 10, 2023, 10:08:44 pm by imperiusdamian »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2054
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Strings in old typed binary files
« Reply #1 on: April 10, 2023, 10:14:58 pm »
You can try it like that: (but I'd suggest to switch to stream usage, for testing I would need a binary file from you.)
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils, Classes;
  7.  
  8. type
  9.    Tinfo = RECORD
  10.       string1, string2, string3, string4 : ShortString;
  11.       int: Integer;
  12.    end;
  13.  
  14. var
  15.    Info: Tinfo;
  16.    OldFile: File of Tinfo;
  17.    NewFile: TStringList;
  18. begin
  19.   AssignFile(OldFile, '.\old.bin');
  20.   Reset(OldFile);
  21.   try
  22.     NewFile := TStringList.Create;
  23.     try
  24.       while not EOF(OldFile) do
  25.         begin
  26.           Read(OldFile, Info);
  27.           NewFile.Add(Info.string1);
  28.           NewFile.Add(Info.string2);
  29.           NewFile.Add(Info.string3);
  30.           NewFile.Add(Info.string4);
  31.           NewFile.Add(IntToStr(Info.int));
  32.         end;
  33.       if (NewFile.Count > 0) then
  34.         NewFile.SaveToFile('.\new.txt');
  35.     finally
  36.       NewFile.Free;
  37.     end;
  38.   finally
  39.     CloseFile(OldFile);
  40.   end;
  41. end.
I do no checking or anything good in that demo, it's just for demonstration purposes.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

imperiusdamian

  • New Member
  • *
  • Posts: 14
Re: Strings in old typed binary files
« Reply #2 on: April 10, 2023, 10:32:23 pm »
Well the code does generate the appropriate text file but it still gives me the same garbled mess. Are FreePascal strings just incompatible with Turbo Pascal ones?

I do have access to an old DOS virtual machine. Perhaps it would be better just to locate a copy of TP7 and do the conversion there, then get the text files from the VM.

Bart

  • Hero Member
  • *****
  • Posts: 5289
    • Bart en Mariska's Webstek
Re: Strings in old typed binary files
« Reply #3 on: April 10, 2023, 10:39:00 pm »
Make sure that type integer in fact is 16-bit.
Depending on the mode you use, it can be 16 or 32 bit in fpc.
(You can use SizeOf(Integer) to check: it should be 2 in your case).

Also notice that alignment of the record may make a difference.
In the original code did you use packed record?
The file size in bytes must be exactly divisible by SizeOf(info).

Another thing: do you have accented characters in the file?

Bart

imperiusdamian

  • New Member
  • *
  • Posts: 14
Re: Strings in old typed binary files
« Reply #4 on: April 10, 2023, 10:52:05 pm »
It is a 16 bit integer, yes.
I did not use packed records.
No accented characters appear in the files.

I picked a rather brute-force solution to the issue: I located an old copy of TP7, installed it on my DOS VM, compiled my program on it, and it worked perfectly.
I guess when all else fails, the simplest approach is sometimes the best.

Thanks for the help and suggestions!  :D

wp

  • Hero Member
  • *****
  • Posts: 11915
Re: Strings in old typed binary files
« Reply #5 on: April 10, 2023, 11:31:42 pm »
The type Integer is 32-bits now, while in the TP days it was 16-bit. As Bart already suggested, redeclare the info record to use a 16-bit integer element (SmallInt), rather than Integer.

Code: Pascal  [Select][+][-]
  1. type
  2.    info = RECORD
  3.       string1, string2, string3, string4 : string;
  4.       int : SmallInt;   // 16-bit integer
  5.    end;

Bart

  • Hero Member
  • *****
  • Posts: 5289
    • Bart en Mariska's Webstek
Re: Strings in old typed binary files
« Reply #6 on: April 10, 2023, 11:52:29 pm »
The type Integer is 32-bits now, while in the TP days it was 16-bit. As Bart already suggested, redeclare the info record to use a 16-bit integer element (SmallInt), rather than Integer.

In default mode (which is {$mode fpc} if I'm not mistaken), SizeOf(Integer) = 2.

It may however well be that SizeOf(Info) in the original TP is not the same as with fpc.
(I get 1024 with fpc in mode fpc or mode tp, which is the same as TP 6.0 gives)

Bart

wp

  • Hero Member
  • *****
  • Posts: 11915
Re: Strings in old typed binary files
« Reply #7 on: April 11, 2023, 12:02:09 am »
Isn't there also a {$mode tp}? Besides the 16-bit integer, this should make also a variable declared as string  behave like in the old days. But I somehow had the impression that the OP wanted to modernize his old TP programs.

cdbc

  • Hero Member
  • *****
  • Posts: 1078
    • http://www.cdbc.dk
Re: Strings in old typed binary files
« Reply #8 on: April 11, 2023, 12:19:23 am »
Hi
I would read the file in a Hex-editor, in that you can see your records. Back then a string was shortstring, lots of times even e.g.: TStr80 = string[80];
I.e.: one byte for length then the string, or length = string[0] text = string[1..n].
You'll be able to see the length bytes and the strings in info-rec in a hex-editor.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Strings in old typed binary files
« Reply #9 on: April 11, 2023, 02:47:50 am »
By any chance, is there TAB characters in the files to compress them?

Also, the Line ending values that are expected, the READLn could be having a melt down..


The only true wisdom is knowing you know nothing

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: Strings in old typed binary files
« Reply #10 on: April 11, 2023, 08:49:52 am »
They are binary files. I would simply expect this to work. We need more details and/or samples from OP.

If it were me, I would start double-checking the structure of the file with an hex editor.

 

TinyPortal © 2005-2018