Difference between revisions of "File Handling In Pascal"

From Lazarus wiki
m (some more typos.)
m (typo)
Line 16: Line 16:
 
</Delphi>
 
</Delphi>
  
IF we only did TFile = File, then it would be impossible to write anything into it! Also, you cannot write integers directly into TFile,  because it is a file of strings. Better use the filetype TextFile for writing values of different types.
+
If we only did TFile = File, then it would be impossible to write anything into it! Also, you cannot write integers directly into TFile,  because it is a file of strings. Better use the filetype TextFile for writing values of different types.
  
 
==IO==
 
==IO==

Revision as of 19:38, 18 August 2010

File handling

Something most programmers need to know how to do is work with files. Files can be used to save user settings, error logs, and more. Here i am going to teach you how to work with basic text files.

About files

When using files in pascal, you can use a TextFile type, which allows you to write string into the file or create your own file type.

<Delphi> ... Type

TIFile = File Of Integer;//Allows you to write Integers into the file
TCFile = File Of PChar;//Write PChars into the file :\
TFile = File Of String;//Write Strings into the file

... </Delphi>

If we only did TFile = File, then it would be impossible to write anything into it! Also, you cannot write integers directly into TFile, because it is a file of strings. Better use the filetype TextFile for writing values of different types.

IO

IO is the file handling thingy for pascal. It is used for getting errors. Since it is a compiler directive, you have to do this: <Delphi> {$I-}//Turn off checking. This way all errors go into the IOResult variable {$I+}//Turn it back on </Delphi>

By disabling (Turning off) IO it all goes into the IOResult variable. This is an cardinal type(Numbers). So, if you want to write it, you have to use the InttoStr function. Different numbers mean different errors. So you may want to check here for the different errors: [1]

File procedures

There are three file procedures you really need to know about

Rewrite - Is used for creating files

Append - Opens an existing file for editing it

Reset - Opens a file for reading it

Example

A full example of handling a text file of type TextFile:

<Delphi> Program FileTest;

{$mode objfpc} //Do not forget this ever

Var

FileVar:TextFile;

begin

 Writeln('File Test');
 AssignFile(FileVar,'Test.txt'); // you do not have to put .txt but this is just for now
 {$I-}
 Try
 Rewrite(FileVar);  // creating the file
 Writeln(FileVar,'Hello');
 Except
 Writeln('ERROR! IORESULT: '+InttoStr(IOResult);
 End;
 CloseFile(FileVar);
 Readln;

end. </Delphi>

Now open the file in any text editor and you will see Hello written to it! You can look all over the internet for examples of the Reset and Append procedures.

Other file procedures

The SysUtils and some other units have procedures for making text files. [2]

OOP

Creating TextFiles in OOP (Object Orientated Pascal) is simple and fast. Mainly because most objects have a SaveToFile procedure.

The usage is similar to that of a TStringList:

<Delphi> Var

Str:TStringList;

begin

 Str := TStringList.Create;
 Str.Add('Hello');
 Str.SaveToFile(<File Path Here>);

end; </Delphi>

I hope this is helpful :D And if anything is wrong or I left something out. Edit At Will!