Hooch
Hooch

Reputation: 29673

Pascal WriteLn failing

I'm working on small project in Pascal for school. I'm using Lazaruz 1.0.2

I have problem with wirteLn function when writing to file. After some time it just stops writing to file.

Take for example this program:

var oFile: Text;
  i: LongWord;
begin
  Assign(oFile, 'test.txt');
  ReWrite(oFile);
  for i:=1 to 4096 do
  WriteLn(oFile, 'ThisIsTest');
  CloseFile(oFile);//Added as suggested
end.

This is output:

...
4072 ThisIsTest
4073 ThisIsTest
4074 ThisIsTest
4075 ThisIsTe

As you can see it just stops at the middle of sentence and it is not writing all. All depends on how long is one WriteLn insturction and how many times it is called.

How to fix it?

I tried to use WinApi function from "Windows" module called WriteFile but I failed to pass last 3 arguments to it.


BIG UPDATE

Thanks. That works (Closing file) in that example. But I have little bit more complex program where I'm passing opened file handle to functions that are writing to it via "var". And even after closing that file at the and does nothing. It is strange.

Upvotes: 0

Views: 776

Answers (2)

John Saltwell
John Saltwell

Reputation: 43

It's also possible to update a file without closing it by adding (in this example)

Flush(oFile); after a Writeln

This is useful where you might have a long file and want to make sure it's updated regularly. Of course, you should still close the file when finished.

Upvotes: 1

Anton Kovalenko
Anton Kovalenko

Reputation: 21507

You should Close(oFile) at the end of your program to be sure the output is flushed.

Upvotes: 5

Related Questions