0

I have an old program that write some pascal record into file :

type
  Character = Record
    Name : String[50];
    Age : integer;
  end;

begin
  // [..] data contain a Character record
  AssignFile(f, data); // example
  Write(f, data); // example
  CloseFile(f) // example
end.

Is it possible to open this file and read record from another language like C, C++, Go ?

Thank you.

Moarz
  • 53
  • 5
  • 3
    Yes, it's possible. – zed May 06 '20 at 09:48
  • It is possible; however, you _will_ need to know what the size of an `integer` is for your Pascal compiler (presumably Delphi, from the tags you've given the question). – Jeff Zeitlin May 06 '20 at 11:04
  • do you have some keywords that can help me for my research ? – Moarz May 06 '20 at 11:32
  • The size of a Delphi `Integer` is 32 bits. (IIRC, that has been the case since Delphi 2, released in 1996. The size is 32 bits in both 32-bit and 64-bit applications.) – Andreas Rejbrand May 06 '20 at 11:42
  • And then there is packing.The first field is 51 bytes, so the second 2-byte field will be on an odd address without padding. – Marco van de Voort May 06 '20 at 21:49
  • @MarcovandeVoort has a good point: you should consider adjusting the string length and adding the `packed` keyword to the record definition. (But you mean "four-byte field", right?) – Andreas Rejbrand May 07 '20 at 08:55
  • Yeah, probably, since it has tag "delphi". In FPC integer varies with mode. – Marco van de Voort May 08 '20 at 10:58
  • @Moarz You can use `sizeof(integer)` to know the size of the integer for the particular compiler & mode. If `sizeof` is not supported, look at the written file, and subtract the known parts. – tonypdmtr May 10 '20 at 21:45
  • Thank you all of you for your answers. It works good. – Moarz May 12 '20 at 08:45
  • I dont know why but sometime I have to skip some unsed bytes (0) between some "random" integers. For exemple : int16, int16, int16, 2 unused bytes, int32, 4 unused bytes, int64, int64, int64. It seems to appear when the next value is an higher integer type. – Moarz May 12 '20 at 09:02

1 Answers1

0

Yes, it is.

You simply get a file with characters and integers. It would be easy to read this file using almost any mainstream programming language.

But of course, the precise code will vary from language to language.

In theory, I could add example code reading such a file in the 100 most common programming languages, but then this answer would become too long.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • If you have a little example in golang, please don't hesitate. – Moarz May 06 '20 at 11:30
  • If you google "golang read binary file" you will find several examples, for instance https://stackoverflow.com/questions/14514201/how-to-read-a-binary-file-in-go. You also need to know [the binary format of Delphi short strings](http://docwiki.embarcadero.com/RADStudio/en/String_Types_(Delphi)#Short_Strings). But how to put this into a nice golang data structure is purely a golang question, and I don't know enough golang to answer that. However, you could ask that as a new question (in the golang tag), if your attempts don't work out for you, and you cannot find that Q already asked. – Andreas Rejbrand May 06 '20 at 11:38