What's new

the readfile function in delphi?

Mayco

Member
I have a problem: when I use the readfile api function in delphi it returns everything what i need except the first byte (when the file begins with "123..." it just returns "23...").

This is the piece of code I use:

procedure openfile();
var
dwread:LongWord;
hFile: THandle;
data: Shortstring;
begin
hFile := createfile(PChar('c:\file.bin'), GENERIC_READ, FILE_SHARE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL + FILE_FLAG_RANDOM_ACCES, 0);
SetFilePointer(hFile, 0, nil, FILE_BEGIN);
Readfile(hFile, data, 64, dwread, nil);
Closehandle(hFile);
end;

c:\file.bin is the file I want to read of course.
data must contain the first 64 bytes of a binary file but it doesn't add the first byte.
 

Cyberman

Moderator
Moderator
Mayco said:
I have a problem: when I use the readfile api function in delphi it returns everything what i need except the first byte (when the file begins with "123..." it just returns "23...").

This is the piece of code I use:

procedure openfile();
var
dwread:LongWord;
hFile: THandle;
data: Shortstring;
begin
hFile := createfile(PChar('c:\file.bin'), GENERIC_READ, FILE_SHARE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL + FILE_FLAG_RANDOM_ACCES, 0);
SetFilePointer(hFile, 0, nil, FILE_BEGIN);
Readfile(hFile, data, 64, dwread, nil);
Closehandle(hFile);
end;

c:\file.bin is the file I want to read of course.
data must contain the first 64 bytes of a binary file but it doesn't add the first byte.
I suggest not setting the file pointer the first time by commenting it out. Then run the code. Also if you have a 'file' position function I would dump that information to a debug text object in some window of your application. Useful for figuring out such things.

Cyb
 
OP
Mayco

Mayco

Member
ok, i have it:

the data: Shortstring; must be data: array[1..64] of Char;
 

Top