delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/12/08/11:32:23

Message-ID: <366D541F.7E73636E@CGSTE.MQ>
Date: Tue, 08 Dec 1998 12:30:23 -0400
From: HANRIGOU <HANRIGOU AT cgste DOT mq>
X-Mailer: Mozilla 4.06 [en] (Win95; I)
MIME-Version: 1.0
To: "djgpp AT delorie DOT com" <djgpp AT delorie DOT com>
Subject: read/write in binary mode: fopen/open behaviour
Reply-To: djgpp AT delorie DOT com

Hi,

I'm trying to port some UNIX code to DJGPP. A piece of the original
code try to copy a binary file. it looks like that:

fin = fopen(input_file, "r");
dout = fopen(output_file, "w");
...
while ((c = read(fileno(fin), buf, sizeof (buf))) > 0)
    for (bufp = buf; c > 0; c -= d, bufp += d)
        if ((d = write(fileno(dout), bufp, c)) <= 0)
            break;

Obviously this code do not work under DJGPP (for binary files) since the 
default file type for read() and write() is TEXT. Nevertheless I thought
just changing first two lines into:

fin = fopen(input_file, "rb");
dout = fopen(output_file, "wb");

whould be enough. Nevertheless this do not work. Changing file mode on
FILE *f do not seem affect the file pointed by fileno(f). This is not
a real problem, because adding 

setmode(fileno(fin), O_BINARY);
setmode(fileno(dout), O_BINARY);

before doing any read() or write() is OK.

Nevertheless is this behaviour correct? Couldn't we expect file mode
changes on FILE *f to be automatically applied to fileno(f)? Please
give me a clue.

-- 
Sorry for my bad english. Spelling corrections are appreciated.

- Raw text -


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