View Issue Details

IDProjectCategoryView StatusLast Update
0037950FPCRTLpublic2020-10-19 10:53
ReporterWolfgang Helbig Assigned ToJonas Maebe  
Status resolvedResolutionno change required 
PlatformimacOSos x 
Product Version3.2.0 
Summary0037950: Buffer is not flushed on textfile other than stdout.
DescriptionIf you have a text file f other than output, it is buffered like all textfiles but the buffer is not always automatically flushed
when the program ends.
TagsNo tags attached.
Fixed in Revision
Attached Files


Jonas Maebe

2020-10-18 15:03

manager   ~0126390

That's correct, you have to close the file for the buffer to be flushed. Files are not automatically closed when a program ends (file handles are closed by the OS, but the buffering happens at a different level).

Wolfgang Helbig

2020-10-18 17:15

reporter   ~0126392

I cannot reproduce the error with a simple program. But with my real program, the very same last chars of output are always missing. Both programs write a char at a time. And both write the same text.

Jonas Maebe

2020-10-18 17:17

manager   ~0126393

Even if you call close(f) at the end? Again: you have to manually close files other than output. The compiler/RTL do not do this automatically for you.

Wolfgang Helbig

2020-10-18 17:48

reporter   ~0126396

Last edited: 2020-10-18 18:59

View 2 revisions

Here is a program, that shows the need of flush:
program pt;
var f: text;
begin assign(f, 'out'); rewrite(f);
write(f, 'a'); writeln(f);

$ fpc flush.p
$ flush
$ wc out
       1 1 2 out
That is 2 bytes, namely a and \lf are properly written to the file out.
And here:
$ wc out
       0 0 0 out
w/o the flush.

Wolfgang Helbig

2020-10-18 19:03

reporter   ~0126403

Last edited: 2020-10-19 10:26

View 2 revisions

comparing programs that need the flush with those that don't need a flush I came to the hypothesis that usage of the put procedure defeats buffering and therefore the need for a flush! I do like this approach! But better yet, when this were documented somewhere.

Marco van de Voort

2020-10-18 21:02

manager   ~0126405

auto flush is only for files in the program statement, e.g.

program pt(input,output,f);
var f: text;
begin assign(f, 'out'); rewrite(f);
write(f, 'a'); writeln(f);

Wolfgang Helbig

2020-10-19 10:53

reporter   ~0126412

Thanks, Marco for sharing your knowledge about the fpc implementation. I retested my hypothesis that put is unbuffered. Accidently my test program did have the file in the program header. So it is not put instead of write that makes the flush unnecessary.
But I think, the RTS does not do an auto flush but the output is unbuffered if and only if the file occurs in the header.
Maybe the MODE ISO might control this as well?

Issue History

Date Modified Username Field Change
2020-10-18 14:44 Wolfgang Helbig New Issue
2020-10-18 15:03 Jonas Maebe Note Added: 0126390
2020-10-18 17:15 Wolfgang Helbig Note Added: 0126392
2020-10-18 17:17 Jonas Maebe Note Added: 0126393
2020-10-18 17:48 Wolfgang Helbig Note Added: 0126396
2020-10-18 18:59 Wolfgang Helbig Note Edited: 0126396 View Revisions
2020-10-18 19:03 Wolfgang Helbig Note Added: 0126403
2020-10-18 21:02 Marco van de Voort Note Added: 0126405
2020-10-18 23:05 Jonas Maebe Assigned To => Jonas Maebe
2020-10-18 23:05 Jonas Maebe Status new => resolved
2020-10-18 23:05 Jonas Maebe Resolution open => no change required
2020-10-18 23:05 Jonas Maebe FPCTarget => -
2020-10-19 10:26 Wolfgang Helbig Note Edited: 0126403 View Revisions
2020-10-19 10:53 Wolfgang Helbig Note Added: 0126412