The fflush function flushes a stream associated with file descriptor fd. If the file associated with fd is open for output, fflush writes to that file the contents of the buffer associated with the stream. If the stream is open for input, fflush clears the contents of the buffer.
fflush returns 0 if the buffer was successfully flushed. The value 0 is also returned in cases in which the specified stream has no buffer or is open for reading only.
Buffers are normally maintained by the operating system, which determines the optimal time to write the data automatically to disk: when a buffer is full, when a stream is closed, or when a program terminates normally without closing the stream. The commit-to-disk feature of the run-time library lets you ensure that critical data is written directly to disk rather than to the operating-system buffers.
Return value:
fflush returns 0 if the buffer was successfully flushed. The value 0 is also returned in cases in which the specified stream has no buffer or is open for reading only.
Example
import standard.C.io
import standard.C.string
define FILENAME "test.txt"
class Main {
function Main() {
if(!(var fd = fopen( FILENAME, "w+" )))
echo "The file " + FILENAME + " was not opened\n";
else {
var ch = "";
var buffer = "new line added after opening the file";
fwrite(buffer, 1, strlen(buffer), fd);
fflush(fd);
fseek(fd, 0, SEEK_SET);
fread(buffer, 1, fsize(fd), fd);
echo "Reading after fflush: $buffer";
fclose(fd);
}
}
}
Results
Reading after fflush: new line added after opening the file