TextFiles MR Long Summarie

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Mr Long Grade:

Subject:
11
Information Technology
Version:
Topic:
Beta
Text Files
Video Education

Mr L ong ON TEXT FILES

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

Writing to a text file


• The following algorithm is used to write to a text file.
• The textfilename will be replaced by whatever the actual name is of the text file.
• There are two scenarios, when you want to create (rewrite) the text file from the
beginning or when you want to add (append) to the current text file.
• This algorithm covers both scenarios.
Sample Code – Writing to 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 add ONE LINE to text file
begin

AssignFile( myFile, ‘textfilename.txt’ ) ; //Link the text file with your text file variable

if FileExists( ‘textfilename.txt’ ) = TRUE then //if the file already exists


begin
Append ( myFile ) ; //add onto currently existing text file
end
else
begin
Rewrite ( myFile ) ; //create a brand file text file (if one did exist, it will be cleared)
End;

//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.

Summary of Text file functions


Function Name Description
AssignFile Links the logical text file variable (myFile) with
Example: AssignFile( myFile , ‘textfilename.txt’ ) ; the physical text file (textfilename.txt) stored in
the folder.
Reset Open existing file (for read only) and sets
Example: Reset( myFile ) ; pointer at beginning of first line.
Program will crash if file does not exist.

2
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Text Files
Video Education

Function Name Description


FileExists Returns TRUE if text file exists in given folder or
Example: if FileExists( ‘textfilename.txt’ ) then FALSE if text file does not exist.
Creates a new text file (for write access) and
Rewrite sets pointer to beginning of first line.
Example: Rewrite( myFile ) ; If file already exists then all contents of the file
will be lost.
Append Opens the existing file (for write access) and
Example: Append( myFile ) ; sets pointer at end of file.
Program will crash if file does not exist.
ReadLn Reads contents of current line in text file into
Example: Readln( myFile , sLine ) ; the string variable (sLine) and moves pointer to
next line.
WriteLn Writes contents of the string variable (sLine)
Example: Writeln( myFile , sLine ) ; into the text file, adds an end-of-line marker
and moves pointer to a new line.
Write Same as Writeln except it doesn’t add an end-
Example: Write ( myFile , sLine ) ; of-line marker. Any content written to text file
next will continue from the same line.
EoF Returns TRUE if pointer is at the end of the text
Example: while NOT EOF ( myFile ) do file or FALSE if pointer is not at the end.
EoLn Returns TRUE if pointer is at the end of the
Example: if EOLN ( myFile ) then current line or FALSE if pointer is not at the end.
CloseFile Closes link between logical text file variable and
Example: CloseFile ( myFile ) ; physical text file in folder. Read or write access
is no prevented.

Components interacting with a text file


• Certain components (example TMemo, TRichEdit, TListbox, TComboBox,
TRadioGroup) have text file procedures that include:
o LoadFromFile ( textfilename : string )
This procedure loads the lines from a text file into the string list of a
component.
Example:
Memo1.Lines.LoadFromFile( ‘Data.txt’ ) ;
Listbox1.Items.LoadFromFile( ‘Names.txt’ ) ;
o SaveToFile ( textfilename : string )
This procedure copies the lines from a component’s string list into a file with
the same name as the given parameter.
Example:
RichEdit1.Lines.SaveToFile( ‘Data.txt’ ) ;
Combobox1.Items.SaveToFile( ‘Names.txt’ ) ;

3
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Text Files
Video Education

Formatting columns in a text file


• When writing to a text file and using the WriteLn procedure, placing a colon and
integer after the string, will allow for that many character spaces (each character
will take one character space) and the text will be aligned to the right.

Example: sName := ‘Harry’ ;


WriteLn( myFile , sName :8 ) ;

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.

Example: sName := ‘Harry’ ;


sName := ‘Smith’ ;
WriteLn( myFile , sName, ‘’:8 – length(sName), sSurname ) ;

H a r r y S m i t h
Explanation:
• WriteLn( myFile , sName, ‘’:8 – length(sName), sSurname ) ;
H a r r y

• WriteLn( myFile , sName, ‘’:8 – length(sName), sSurname ) ;


H a r r y

8 spaces for sName column. Add the number of


spaces left after the name ( 8 – length(sName) )

• WriteLn( myFile , sName, ‘’:8 – length(sName), sSurname ) ;


H a r r y S m i t h

Add the sSurname next

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

For more IT related material find us on:

youtube.com/user/MrLongEducation

facebook.com/MrLongEducation @MrLongEdu

You might also like