Jump to content

[PASCAL] Creating and writing into text file


CouldnoT
 Share

Recommended Posts

Creating and writing into text file

 

var
 FileName: string;
 F: TextFile;
 Line: string;
 ReadyToCreate: Boolean;
 Ans: Char;
 i: Integer;
begin
 Write('Input a new file name: ');
 Readln(FileName);
 // Check if file exists, warn user if it is already exist
 if FileExists(FileName) then
 begin

 Write('File already exist, did you want to overwrite it? (y/n)');
 Readln(Ans);
 if upcase(Ans) = 'Y' then
 ReadyToCreate:= True
 else
 ReadyToCreate:= False;
 end
 else // File does not exist
 ReadyToCreate:= True;
 if ReadyToCreate then
 begin
 // Link file variable (F) with physical file (FileName)
 AssignFile(F, FileName);
 Rewrite(F); // Create new file for writing

 Writeln('Please input file contents line by line, '
 , 'when you finish write % then press enter');
 i:= 1;
 repeat
 Write('Line # ', i, ':');
 Inc(i);
 Readln(Line);
 if Line <> '%' then
 Writeln(F, Line); // Write line into text file
 until Line = '%';
 CloseFile(F); // Release F and FileName connection, flush buffer
 end
 else // file already exist and user does not want to overwrite it
 Writeln('Doing nothing');
 Write('Press enter key to close..');
 Readln;
end.

 

In this example, we have used many important things:

 

1. Boolean type:

  ReadyToCreate: Boolean;

This type can hold only one of two values: either True or False. These values can be used directly in if condition, while loop or repeat loop. In the previous example, we have used the if condition like this:

  if Marks[i] > Max then


Which eventually turns to True or False.

 

2. UpCase function:

 if upcase(Ans) = 'Y' then

This statement is executed when the file exists. The program will warn the user about overwriting an
existing file. If he/she wants to continue, then he/she should enter a lowercase y or capital Y. The
UpCase function will convert the character into a capital letter if it is lowercase.


3. Rewrite procedure:

 Rewrite(F); // Create new file for writing

The Rewrite procedure is used to create a new empty file. If the file already exists, it will be erased and overwritten. It also opens the file for writing only in case of text files.

 

4. Writeln(F, ..) procedure:

 Writeln(F, Line); // Write line into text file

This procedure is used to write string or variables in text file, and appends them with end of line characters, which are a carriage return/line feed combination (CR/LF), represented as the characters for the numbers 13 and 10 respectively. These characters can not be displayed in a console window, but it will move the screen display cursor to a new line.


5. Inc procedure:

 Inc(i);

This procedure increases an integer variable by one. It is equivalent to the statement:

 i:= i + 1;

 

6. CloseFile procedure:

 CloseFile(F); // Release F and FileName connection, flush buffer

As we mentioned earlier, the CloseFile procedure releases a file in the operating system. In addition, it has an additional job when writing to a text file, which is flushing the writing buffer.

 

Buffering of text files is a feature that makes dealing with text files faster. Instead of writing a single line or character directly to disk or any other storage media (which is very slow compared with writing into memory), the application will write these entries into a memory buffer. When the buffer reaches its full size, it will be flushed (forced to be written) into permanent storage media like a hard disk. This operation makes writing faster, but it will add the risk of losing some data (in the buffer) if the power is suddenly lost. To minimize data loss, we should close the file immediately after finishing writing to it, or calling the Flush procedure to flush the buffer explicitly.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.