PDA

View Full Version : End of file



botmaster
11-02-2006, 08:59 PM
The problem:

I need to open a file and read it as string till the end of the file. How could I do something like that? I know how to open files, and how to read a certain # of characters of it, but not how to get to the end of the file without causing a memory access violation. Any help?

The code so far:

function ReadFile(fileno : integer): string;
var
i: integer;
begin
repeat
ReadFileString(fileno, result, i);
i:= i+1;
until(EndOfFile(fileno)=true);
end;

CamHart
11-03-2006, 06:05 AM
Filenum:= OpenFile(Path, true);
if(Filenum >= 0)Then
Begin
ReadFileString(Filenum, TheString, FileSize(Filenum));
End;
CloseFile(Filenum);


Try that maybe... You're thinking about reading bytes, not strings. A byte would be like the key "a" or w/e (i think atleast), a string is "asdflkjhadslkfjahlskdjhfaskgnuwiglaksdjnfg" but could also be "a" if written correctly.

botmaster
11-05-2006, 10:31 AM
TYVM!!! This stuff really helped me out. Didn't know there was a FileSize Function.:duh: Thanks a lot!

Ron
11-09-2006, 05:47 PM
You can just try this function if you like. Just type in ReadFile and then the path to the file and run the script. :)



function ReadFile(Path : String) : String;
var
FileNum : Integer;
Text : String;
begin
FileNum := OpenFile(Path, True);
if(Filenum > -1)then
begin
ReadFileString(Filenum, Text, FileSize(FileNum));
end;
CloseFile(FileNum);
Result := Text;
end;


~Ron