Work with text file

From Delphi Pascal wiki
Jump to navigation Jump to search

How on TEXT FILES?

For work with a files, we need to definited a handle at first. We can do that in definition of cariables. File is a kind of file variable.

var f:text;        { text file }

Hadle we connect to a file, which name we kno, its fo PC to know what kind of file we want to use. We get it to the command of assign. Next we will work with a file only with help of F variable.

Assign (f,'C:\subor.txt');

For next work we need to know, what we want to do ...read or write to the file. For any kind is a special commands of opening here.. For read, we use a ReSet ,and (handle) to the. For write we use ReWrite and handle.

ReSet(f);        { for reading }
ReWrite(f);      { for writing }

Next we use only a read and write ( Writeln or Readln).

Further work using a set of commands Write or Read (or WriteLn and ReadLn). If you want to use to write write. Normally used to extract Write text on the screen. However, if the first parameter lists cartwheel handle, so everything will write to the file. Read analogy works. All you would have to enter from the keyboard will be read from a file. When read as the first parameter also lists handle. Shows the difference between writing to the screen and written to a file.


Ukazuje rozdiel medzi zápisom na obrazovku a zápisom do súboru.

Write('Text on monitor');
Write(f,'Text to file');    { f is a handle of file }

Read(i);      { Read a variable from keyboard }
Read(f,i);    { read from file }


ATTENTION:Write we use only for a file, which we have to pened before, ina command of ReWrite. Read only for files from a ReSet command.

If we want to read a file, program cannot know, where is a end of a file. For this, we have a EoF command (End of File), which we get to a condition of our cycle - while or repeat. Function of EoF is true, if we have the end of file in other, is a False.

ReSet(f);
while ( not( eof(f) )) do begin
   ReadLn(f,s);
   WriteLn(s);
end;

At the end, we need to end a work wih a file, so we CLOSE the file for save our progress.

Close(f);


Recapitulation.

  • ASSIGN - Connect a file with handle
  • RESET - Reading
  • REWRITE - Writing
  • WRITE - Writing to a file
  • READ - Reading from a file
  • EOF - Checking end of file with Read
  • CLOSE - End of connection


Two Examples

Example 1: Open a file and write to it "Hello world".

program write_to_file;
var f:text;
begin
   Assign(f,'c:\subor.txt');
   ReWrite(f);
   Write(f,'ahoj svet');
   Close(f);
end.

Example 2: Open a file, and write all o fit to the screen.

program for_reading_from_file;
var f:text;
       s:string;
begin
   Assign(f,'c:\subor.txt');
   ReSet(f);
   while ( not( eof(f) )) do begin
       ReadLn(f,s);
       WriteLn(s);
   end;
   Close(f);
end.


Annotation:

  • For work with more files, its good to have for every a handle, which we can use to work with it.
  • If we used ReWrite for an existing file,this file will be deleted and it will make a new, empty.
  • For check only for an end of a line, we can use a EOLN command.
  • Another command is APPEND. From a name we can know i tis for adding of text to the end of a file.
  • For work with a .dat files is using a variable of :file of xy" and the other commands, See in a next chapter.


Where to go: Content | Attachments