Solved

Read/write binary files

Posted on 1997-03-29
3
169 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
  • 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

15 Experts available now in Live!

Get 1:1 Help Now