Lazarus – the Pascal way for files

 


Since Turbo Pascal, there's been a standard way to read and write data to a file. Text files. That's what I'm talking about. As a Pascal fan, I really wanted to put this method to use. Within Pascal, there are several actions you can perform with a file: read data from a file, write data to a file, rename a file, delete a file.


 

Writing to a File

First, you need a variable of type TextFile. And a string, meaning a string data type.

var

MyTextFile:TextFile;

MyString:String;

Now we'll associate the variable and the file path.

AssignFile(MyTextFile,'Example.Txt');

When you're finished working with the file, you need to close the file.

CloseFile(MyTextFile);

Now you can write information to the file. To do this, you need to specify the file mode. In Pascal, this is ReWrite. Writing to a file. If such a file doesn't exist, it will be created.

ReWrite(MyTextFile);

Now we write to the file using WriteLn. This is the most common command in Pascal. We'll specify the text file as the first parameter. As a variable.

WriteLn(MyTextFile,'Hello World!');



Reading from a File

Reading from a text file. It's done in much the same way as writing to a file. But it's a little trickier. Because the file needs to be read. We need to know the end of the file and take the data from there line by line. This is a loop.

We also associate a variable of type text file and the path to the file.

AssignFile(MyTextFile,'Example.txt');

Let's specify the reading mode. This time, it's the Reset command. This means we'll read lines from the file.

Reset(MyTextFile);

Now we can begin the loop. To read lines from the file, we'll read line by line. How do we know where the end of the file is? We need to read all the lines and get to the end of the file. That's what the Eof.End of File command is for.

Eof(MyTextFile)

Just make sure to include a loop that reads data until we reach the end of the file.

while (not Eof(MyTextFile)) do

This is the loop condition for reading.

Inside this loop, we read the lines with the ReadLn command. This is also a standard and familiar command. The first parameter is the name of the text file type (TextFile).

ReadLn(MyTextFile,MyString);

Don't forget to close the file when you're finished reading. Always close the file when you're finished working with it.

CloseFile(MyTextFile);



Program code:

var

MyTextFile:TextFile;

MyString:String;

begin

//1 Writing to a text file, Pascal method

AssignFile(MyTextFile,'Example.Txt');

ReWrite(MyTextFile);

WriteLn(MyTextFile, 'Hello World!');

WriteLn(MyTextFile, 'Hello World!');

WriteLn(MyTextFile, '1234567890+-?');

CloseFile(MyTextFile);

 

//2 Reading a text file, Pascal method

Memo1.Clear();

Memo1.Lines.Add('Reading from file:');

AssignFile(MyTextFile, 'Example.txt');

Reset(MyTextFile);

while (not Eof(MyTextFile)) do

begin

ReadLn(MyTextFile, MyString);

Memo1.Append(MyString);

end;

CloseFile(MyTextFile);

Dima Link is making retro videogames, apps, a little of music, write stories, and some retro more.


WEBSITE: http://www.dimalink.tv-games.ru/home_eng.html 

ITCHIO: https://dimalink.itch.io/


TUMBLR: https://dimalink.tumblr.com/

BLOGGER: https://dimalinkeng.blogspot.com/

MASTODON: https://mastodon.social/@DimaLink


Comments

Popular posts from this blog

Jedi Knight Dark Forces II – cinematic action and creepy aliens

Udochka – about flickering screen, Pascal moment