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
Solved

Read/write binary files

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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