Read bigger and small text file delphi
Written by Codes Tips on March 18, 2009 – 10:08 am -Read small text files in Delphi
First we should declare a variable of type TextFile like this
var f:textfile;//defines variable for type TextFile for maintaining data from a file.
We will need also other variables to read the file:
var s:string; ch:char;
If you wanna read contents of a file in Delphi you should first assign to a variable the name of the file you wanna read. You can use the following statement:
assignfile(f,path+'filename.txt');// where path can be the path to the directory
If the file you wanna read it’s inside the folder project then you can use the following code:
path:= ExtractFilePath(application.exename);//ExtractFilePath it's a function that returns a string with the path of an .exe
Next if you should let the compiler know that you wanna read the file.
reset(f);
Now you have more options to read a file depending on what your needs are or how bigger it’s the file from where you read.
You can use the following code to read the file character by character(this version that is not faster to read files):
while not eof(f) do begin read(f,ch); s:=s+ch; end;
You can also use
readln(f,s)
which will read a whole line from the input file.
In the end you should close the file that you opened.
closefile(f);
Complete code, function to return contents of a file:
function ReadSmallFile:string; var freadfile:textfile; s,path:string; ch:char; begin path:= ExtractFilePath(application.exename); assignfile(freadfile,path+'filename.dat'); reset(freadfile); while not eof(freadfile) do begin read(freadfile,ch); s:=s+ch; end; closefile(freadfile); ReadSmallFile:=s; end;
Read bigger file in Delphi
For reading bigger files in Delphi you should use the function BlockRead. This function is used to read blocks of data into a buffer from a file.
To declare a file you should use:
var biggerfile:file of char; BufArray: array[1..4096] of Char;//we will read 4 KB at a time nrcit,i,:integer; sir:string;
You should after assign to a variable just like the type TextFile:
assignfile(biggerfile,path+'namefile.dat'); reset(biggerfile);
Here is a statement to read a bigger file of type char
repeat blockread(biggerfile,BufArray,SizeOf(BufArray),nrcit); for i:=1 to nrcit do sir:=sir+BufArray[i]; until (nrcit = 0);
To close the file you should use
closefile(fis);
Here is a complete source code function to read a bigger file:
function ReadBiggerFile:string; var biggerfile:file of char; BufArray: array[1..4096] of Char;//we will read 4 KB at a time nrcit,i:integer; sir,path:string; begin path:= ExtractFilePath(application.exename); assignfile(biggerfile,path+'namefile.dat'); reset(biggerfile); repeat blockread(biggerfile,BufArray,SizeOf(BufArray),nrcit); for i:=1 to nrcit do sir:=sir+BufArray[i]; until (nrcit = 0); closefile(biggerfile); ReadBiggerFile:=sir; end;
Tags: borland delphi, Delphi, programming
Posted in Delphi |
Leave a Comment
You must be logged in to post a comment.


















