Lazarus – Pascal Method for File 2

 


We continue to learn Turbo Pascal and Lazarus programming. I'm learning too. Developing. Quite slowly. Studying topics. Practicing. Oh, it's such a long process. I need to take breaks. And constantly return to the world of Turbo. Turbo Pascal, Delphi, Lazarus, Free Pascal.


Now, here are the additional features. The most important is reading and writing. That's all the basics. And here's an additional lesson. For example, how to append to a file. How to delete a file. And how to rename a file. Pascal and Lazarus can do that too!

Appending to a File

Here, the file already exists. And just add lines to it. We're working with a text file. It's similar to writing to a file. And essentially the same code. Except for the file mode. When we were writing to a file, the file mode was called ReWrite. And now we're appending to a file. And this mode is called Append. We'll simply add a new line to an existing file.

In the program section where variables are declared, the var section, we have a text file. The data type is text file. We're working with it.


var

MyTextFile:TextFile;

MyString:String;


We also associate a variable of type text file (MyTextFile) with the file itself (just the name and path to the file).


AssignFile(MyTextFile,'Example.Txt');


And of course, don't forget. Once you've opened the file, you've associated the variable and the file. Now, close the file at the very end. You can write directly.


CloseFile(MytextFile);


We set the file mode. This is new. But it's similar to reading a file. Only the mode is different.


Append(MyTextFile);


And now—the last thing. The simplest thing. We simply write lines to the file. Using the WriteLn command. And here, the first parameter is a variable of type text file. We're essentially writing to the file. And the second parameter is the string we want to write.


WriteLn(MyTextFile, 'I'm appending line 1 to the file');

WriteLn(MyTextFile, 'I'm adding string 2 to the file');


And so we write to the file as many times as we want. New lines will appear in the file. We write to the end of the file.



Rename the file

You can also rename a file directly in the programming environment. It's very easy to do. Use the RenameFile command. Rename from this name to this new name. There's no need to AssignFile or CloseFile here. It's just the command itself. Just write it. The file must exist.

For example,


RenameFile('Example2.txt', 'NewExample2Name.txt');


Delete the file

Similarly, you can delete the file. The simple DeleteFile command. We pass the path to the file as a parameter. It's very simple. And everything will work right from the development environment.

For example,


DeleteFile('Example3.txt');


These are the methods for working with a file. For all Turbo fans! Turbo Pascal, Delphi, Lazarus, Free Pascal.



Program code:


var

MyTextFile:TextFile;

MyString:String;

begin

//3 append to file

AssignFile(MyTextFile,'Example.txt');

Append(MyTextFile);

WriteLn(MyTextFile,'I'm appending line 1 to the file');

WriteLn(MyTextFile,'Adding new string 2 to a file!');

CloseFile(MytextFile);

 

//4 rename the file

AssignFile(MyTextFile,'Example2.txt');

ReWrite(MyTextFile);

WriteLn(MyTextFile,'Something is here!');

CloseFile(MyTextFile);

RenameFile('Example2.txt','NewExample2Name.txt');

 

//5 Delete file

AssignFile(MyTextFile,'Example3.txt');

ReWrite(MyTextFile);

WriteLn(MyTextFile,'dlkdffsjdlkfsjdlfksdfsdf');

CloseFile(MyTextFile);

DeleteFile('Example3.txt');

End.


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