[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Read/write binary files

I'm trying to read the contents of a binary file into an array of records. e.g
 FileRead(FileHandle, TeamRecord[i], SizeOf(TeamRecord[i]));

The first field of the record is a string[25], and for some reason, the first letter of the string is not being read into the record. I'm new to Delphi - have I done something wrong??
0
ChrisGin
Asked:
ChrisGin
  • 2
1 Solution
 
sperlingCommented:
This is probably because the string in the file is 0-terminated.

A pascal string stores the length of the string in the first character of the string, and then the actual data. (Not true for the D2 type STRING without any length specifier)

'ABC' pascal string :  #3 #65 #66 #67
'ABC' 0-terminated  :  #65 #66 #67 #0

What you should do, is to declare the TeamRecord type something like this:

type
  TTeamRecord =
    AString   : ARRAY [0..24] OF CHAR;
    .
    .

Or, maybe its ARRAY [0..25], depends on the binary file format.


If this didn't solve it, leave me a comment along with your record declarations and a bit more source code.

Regards,

Erik.


0
 
ChrisGinAuthor Commented:
Thanks! That solved the problem. However, by making it an array of characters, will this mean that I can no longer perform string operations on that field? If that's true, is there a conversion function I can use to convert an array of characters to a string?
0
 
sperlingCommented:
Yep.

StrPas(a) returns a string, if 'a' is either a PChar or a 0-terminated array of char

Regards,

Erik.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now