Solved

Read/write binary files

Posted on 1997-03-29
3
192 Views
Last Modified: 2010-04-06
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
Comment
Question by:ChrisGin
[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
3 Comments
 
LVL 3

Accepted Solution

by:
sperling earned 50 total points
ID: 1335057
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
 

Author Comment

by:ChrisGin
ID: 1335058
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
 
LVL 3

Expert Comment

by:sperling
ID: 1335059
Yep.

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

Regards,

Erik.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

726 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