0
votes

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.

2
This is functionality that was stable in 1997. If the complex scenario still fails, there must be another bug. Keep simplifying till the problem goes away. Then you have a fair chance that your last change is related to the bugged spot. - Marco van de Voort
@MarcovandeVoort For all that time I wasn't able to fix it. But it is the last Pascal project that I will see in my life. I even tried to use Winapi functions to write to file but it is impossible for Pascal to pass NULL, NIL or 0 as argument. And because of that it is impossible to use winapi function WriteFile. - Hooch

2 Answers

5
votes

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

1
votes

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.