Professional Documents
Culture Documents
TextFiles MR Long Summarie
TextFiles MR Long Summarie
TextFiles MR Long Summarie
Subject:
11
Information Technology
Version:
Topic:
Beta
Text Files
Video Education
Text file
• File (with the .txt extension) used to store data to be used later by a program
• Contains no formatting.
You can create a text file by:
In windows explorer
• Right click inside a folder a select New –> Text
Document
In Delphi
• Click File -> New -> Other
• Select Other File option, select Text File and
click OK.
Reading from a text file
• The following algorithm is used to read the contents of a text file.
• The text file MUST be saved in the same folder as your Delphi files.
• The textfilename will be replaced by whatever the actual name is of the text file.
• The loop will start from the beginning of the text file, reading in each line
individually, until the end of the text file.
Sample Code – Reading from a text file
var myFile : TextFile ; //declare a text file variable to link to your text file
sLine : string ; //this string will be used to read in EACH LINE of text file
begin
if FileExists( ‘textfilename.txt’ ) = FALSE then //use FileExists to see if the file does not exist
begin
showmessage( ‘File not found!’ ) ; //error message
Exit ; //stops the rest of the procedure code
end;
AssignFile( myFile, ‘textfilename.txt’ ) ; //Link the text file with your text file variable
Reset( myFile ) ; //puts a pointer at the beginning of the first line of the text file
while NOT eof( myFile ) do //Keep looping while you are NOT at the End Of File
begin
readln( myFile, sLine ) ; //read in CURRENT LINE into sLine, move pointer to next line
//Here you work with sLine, doing what is required when reading ONE line from text file
end; //end of while
CloseFile( myFile ) ; // Close access to the file
1
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Text Files
Video Education
var myFile : TextFile ; //declare a text file variable to link to your text file
sLine : string ; //this string will be used to add ONE LINE to text file
begin
AssignFile( myFile, ‘textfilename.txt’ ) ; //Link the text file with your text file variable
//Construct sLine to contain one line you want to add to text file
writeln( myFile, sLine ) ; //add contents of sLine into text file, move pointer to next line
//continue using writeln to add more to text file
CloseFile( myFile ) ; // Close access to the file
If you want to create a brand new file every time and don’t want the contents that are currently in the text
file, then you only need to Rewrite and not use Append.
If you know that the file will exist, and you want to add to the current contents, then you only need to
Append and not use Rewrite.
An error will occur if you attempt to Append a text file that does not exist.
2
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Text Files
Video Education
3
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Text Files
Video Education
H a r r y
• You can format the text that is written into a text file into columns be placing the
correct number of “empty” characters between one column.
H a r r y S m i t h
Explanation:
• WriteLn( myFile , sName, ‘’:8 – length(sName), sSurname ) ;
H a r r y
4
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Text Files
Video Education
Additional Links:
• Youtube video playlist:
https://www.youtube.com/watch?v=wheQ-Vjmqk4&list=PLxAS51iVMjv9YfoklB8aTbelOmcxV7m3i
• Google drive resource activities:
https://tinyurl.com/MLE-G11IT-TextFiles
youtube.com/user/MrLongEducation
facebook.com/MrLongEducation @MrLongEdu