delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2013/12/02/21:38:28

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
X-Recipient: djgpp AT delorie DOT com
Message-ID: <529D448A.7050208@gmx.de>
Date: Tue, 03 Dec 2013 03:40:10 +0100
From: Juan Manuel Guerrero <juan DOT guerrero AT gmx DOT de>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.1.0
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Question about different behavior of fclose/fread on plain dos and
windows.
X-Provags-ID: V03:K0:fzGL4J3Xo0ChiC++Buu0RHjf1t3+eeXwd6Ob9DF2UhManvsaKW+
SFY3saZcCrsfFckYguYu8nZaFGBjt/eNRQILPWQq8V1Lh5ifBnrCLOMCSGsFcyXi8dPZBaB
HHRnE3UClukEy9ko6aajbBfa0tVpd40HCSGrznw0iEteAkHmR2ic8KrSi9DObavYhbFAqGf
YT1EMKseTb2D2vSBEQJTA==
Reply-To: djgpp AT delorie DOT com

Please inspect the code snippet below:

#include <stdio.h>

int main(void)
{
   char buf_w[] = "pipapo";
   char buf_r[sizeof(buf_w)] = "";
   FILE *fw = fopen("file.txt", "w");
   FILE *fr = fopen("file.txt", "r");
   size_t bytes;

   bytes = fwrite(buf_w, sizeof(char), sizeof buf_w, fw);
   printf("fwrite(fw) %s (%d): %s\n", bytes != sizeof buf_w ? "KO" : "OK", bytes, buf_w);
   bytes = fread(buf_r, sizeof(char), sizeof buf_r, fr);
   printf("fread(fr) %s (%d): %s\n", bytes != 0 ? "KO" : "OK", bytes, buf_r);

   fclose(fw);
   fseek(fr, 0, SEEK_SET);
   bytes = fread(buf_r, sizeof(char), sizeof buf_r, fr);  /* This cuases difficulties. */
   printf("fread(fr) %s (%d): %s\n", bytes != sizeof buf_r ? "KO" : "OK", bytes, buf_r);

   return 0;
}


The code shall open the same file for writing and reading.  I have compiled
the code on linux and on Win98SE, Win2K and WinXP.  On windows I have used
DJGPP 2.03 and DJGPP 2.04.  In all these cases the code works as I expected.
This means that reading after having closed the file shall result in a filled
buf_r.  The program gives the following output:

fwrite(fw) OK (7): pipapo
fread(fr) OK (0):
fread(fr) OK (7): pipapo


But if I compile the same code on plain DOS (MSDOS 6.22 or MSDOS 7.1) the program
fails producing the following output:
fwrite(fw) OK (7): pipapo
fread(fr) OK (0):
fread(fr) FO (0):


(If you run the program twice on plain dos remove the produced file.txt before
starting the program or the program will seem to work.)
As can be seen the the reading file struct does not note that the writing file
struct has changed/updated the file content.  The fact that really surprises me
is the different behavior of the dos emulation of windows and the plain dos behavior.
Am I missing something here?  Is this the normal dos behavior or must the library
be fixed?  Is there a way to fix the library so that the behavior on plain dos
becomes the same than on windows?

The code above is not arbitrary but it is the code produced by lua for the following
lua code snippet:

   local f = assert(io.open(file, "w"))
   local fr = assert(io.open(file, "r"))
   assert(f:setvbuf("full", 2000))
   f:write("x")
   assert(fr:read("*all") == "")  -- full buffer; output not written yet
   f:close()
   fr:seek("set")
   assert(fr:read("*all") == "x")   -- `close' flushes it

Forget about the assert function call.  This code is mapped by the lua
interpreter in the following C code sequence:
   FILE *fw = fopen("file.txt", "w");
   FILE *fr = fopen("file.txt", "r");

   fwrite(buf_w, sizeof(char), sizeof buf_w, fw);
   fclose(fw);
   fseek(fr, 0, SEEK_SET);
   fread(buf_r, sizeof(char), sizeof buf_r, fr);


Regards,
Juan M. Guerrero

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019