1

Consider the following simple code to creat a text.sys TEXT file.

program MakeAFile;
var
 NewFile : TEXT;
 DummyVar : INTEGER;   
begin
 assign(NewFile,'text.sys');
 rewrite(NewFile);
 writeln('Some text...');
 DummyVar : 3;
 writeln('Some other text...');
 close(NewFile);
end.

My question is what the NewFile variable holding in the entire runtime? Did it hold the "Some text..." then transfer that to the disk then continue hold the "Some other text..." and transfer it to the disk,consecutively like that? Or it held both of the line and when the close(NewFile) is called then it transfer the two line at once? And if it the second case,then how the variable manage two value like that? I mean then it has to be some memories to reserved for the NewFile variable since the file can be unknown large. Thank you!

1
  • The two lines with writeln miss NewFile as the first parameter to writeln. Commented Jun 17, 2016 at 15:28

2 Answers 2

3

The following should work. To write to the file, you should use it as the first parameter of Writeln or Write. Otherwise the text is written to the standard output (usually the console).

program MakeAFile;
var
  NewFile: Text;
  Dummyvar: Integer;
begin
  Assign(NewFile,'text.sys');
  Rewrite(NewFile);
  Writeln(NewFile, 'Some text...');
  DummyVar := 3;
  Writeln(NewFile, 'Some other text...');
  Writeln('This appears on the console');
  Close(NewFile);
end.

How does it work?

File and Text are types that in reality point to a complex record, which contains, for instance, a file handle or similar, probably a write and/or read pointer into the file, and perhaps some other bookkeeping stuff, like the file name (if that is required on a specific platform), etc. Text types also contain a buffer and some extra bookkeeping data for that buffer (e.g. buffer read or write pointer).

The text you write to the file is actually written to the buffer (because that is much faster than writing to a file) and, each time the buffer is full, the text in the buffer is written to the file and the buffer is cleared. On Close(), the remaining text in the buffer is written to the file (i.e. the buffer is flushed) and the file is closed.

The use of the buffer means that not for each line written, the file must be accessed (because that is much slower than writing to the memory of a buffer), but only when the buffer is full.

Sign up to request clarification or add additional context in comments.

Comments

1

When you do NewFile : TEXT;, you say to the compiler to allocate some space in memory to create a text buffer. When you write to the file, you do not actually write to the disk, you write to the buffer. If the buffer gets full or if you close the file, the buffer is written to the disk.

You have a procedure Flush to empty the buffer manually: FP documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.