fopen("myfile",r) or fopen("myfile",rb): pro/cons ? 
Author Message
 fopen("myfile",r) or fopen("myfile",rb): pro/cons ?


Quote:
>Hi,

>I'm still new to C and I don't see clearly the difference between opening a
>read-only file in text or binary mode. I just noticed that when pgming a
>filter for a certain kind of file which could contain in the middle an
>end-of-file caracter, opening it in plain text mode had the unpleasant effect
>to truncate it at read !

The theoretical reason is that "text" means that the file is to be read
in some OS-defined way - e.g., under MSDOS, lines end in CR/LF pairs and
^Z functions as a logical end-of-file marker (that is, data after the ^Z
is not logically part of the file).  Needless to say, neither of these
is relevant under Unix.  And, if all you using is Unix, there is no
difference - so you will not notice any difference - this is a product
of C's Unix heritage.

Quote:
>So, why not always open all my files in binary mode just in case ? Analysing
>both open mode with a profiler shows no difference in the read speed.

Because if you want your code to run under other, less friendly OS's,
you will not want to have to parse their weird record/blocking/eof'ing
conventions yourself.  Opening in text mode ensures that your code can
be compiled in new environments and that it is the responsibility of the
compiler developers on that platforms to ensure that it does the right
thing.

************************************************************************
"The rich are different from you and me."
                - Fitzgerald -
"Yes.  They have more money."
                - Hemmingway -


          hundreds, if not thousands, of dollars, every time he posts -
************************************************************************
rwvpf wpnrrj ibf ijrfer



Sat, 20 Jul 1996 01:53:20 GMT  
 fopen("myfile",r) or fopen("myfile",rb): pro/cons ?
Hi,

I'm still new to C and I don't see clearly the difference between opening a
read-only file in text or binary mode. I just noticed that when pgming a
filter for a certain kind of file which could contain in the middle an
end-of-file caracter, opening it in plain text mode had the unpleasant effect
to truncate it at read !

So, why not always open all my files in binary mode just in case ? Analysing
both open mode with a profiler shows no difference in the read speed.

                                       Thank you for your answer,
                                            Jean-Pierre
---------------------------------------------------------------------------

Assistant                                            tel : +41.21.692.41.65
HEC-Inforge                                          fax : +41.21.692.41.50
University of Lausanne                                 attn. J.-P. Schnyder
CH-1015 Lausanne-Switzerland                    



Fri, 19 Jul 1996 22:18:57 GMT  
 fopen("myfile",r) or fopen("myfile",rb): pro/cons ?

Quote:
>Hi,

>I'm still new to C and I don't see clearly the difference between opening a

           ^^^^^^^^
Well, that's pretty obvious, otherwise you would have written the subject
line correctly :-)

Quote:
>read-only file in text or binary mode. I just noticed that when pgming a
>filter for a certain kind of file which could contain in the middle an
>end-of-file caracter, opening it in plain text mode had the unpleasant effect
>to truncate it at read !

>So, why not always open all my files in binary mode just in case ? Analysing
>both open mode with a profiler shows no difference in the read speed.

Don't expect any system to behave like yours. If I'm not wrong, there
are implementations where you can't split a text file in lines unless
you open it in text mode.

If a file has been created as a text file and has to be processed as a
text file, it's quite sensible to open it in text mode. Otherwise, open
it in binary mode.

If your platform defines an end-of-file caracter and a text file contains
this character in the middle then either this file is not a text file
and opening it in text mode is an error or the logical end of that text
file is at that point and you have no reason to complain.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Sat, 20 Jul 1996 01:31:04 GMT  
 fopen("myfile",r) or fopen("myfile",rb): pro/cons ?


Quote:
>I thought that there was also an issue of buffering? That is, text
>files will have buffers, and binary files won't.

No, text/binary has nothing to do with buffering. You're probably thinking
of the default buffering that's often used when you open a file (e.g.
stdout line buffered to a terminal, fully buffered to a file, stderr
unbuffered).

Text/binary is simply whether the I/O library performs any conversions
between the format used by the OS to represent a line and that used
internally by C programs (i.e. a variable length \n terminated record).

-----------------------------------------


-----------------------------------------



Sun, 21 Jul 1996 05:26:23 GMT  
 fopen("myfile",r) or fopen("myfile",rb): pro/cons ?

Quote:
>I thought that there was also an issue of buffering? That is, text
>files will have buffers, and binary files won't.

>Or am I thinking of something else? This is a vague memory...

Yes, you're thinking of something else, but don't ask me of what.

Both text and binary files are buffered by the stdio routines, unless
the programmer had explicitly disabled this buffering. And most OS's
are buffering the files at a lower level too, and this buffering cannot
be disabled with ANSI C calls (or it cannot be disabled at all).

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Sun, 21 Jul 1996 17:45:27 GMT  
 fopen("myfile",r) or fopen("myfile",rb): pro/cons ?


    >> Hi,
    >>
    >> I'm still new to C and I don't see clearly the difference between opening a
    >> read-only file in text or binary mode. I just noticed that when pgming a
    >> filter for a certain kind of file which could contain in the middle an
    >> end-of-file caracter, opening it in plain text mode had the unpleasant effect
    >> to truncate it at read !

    Jeremy> The theoretical reason is that "text" means that the file is to be read
    Jeremy> in some OS-defined way - e.g., under MSDOS, lines end in CR/LF pairs and
    Jeremy> ^Z functions as a logical end-of-file marker (that is, data after the ^Z
    Jeremy> is not logically part of the file).  Needless to say, neither of these
    Jeremy> is relevant under Unix.  And, if all you using is Unix, there is no
    Jeremy> difference - so you will not notice any difference - this is a product
    Jeremy> of C's Unix heritage.

I thought that there was also an issue of buffering? That is, text
files will have buffers, and binary files won't.

Or am I thinking of something else? This is a vague memory...
--

 (301) 286-2396                          fax: (301) 286-1754



Sat, 20 Jul 1996 21:25:07 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. remove() vrs fopen("""w")

2. fopen "r" and "rb" question

3. fopen, freopen "a+" mode

4. "fopen" che non fa open

5. problem about "fopen"

6. fopen question ["r+"]

7. 250 times 'fopen(...,"a")':errno 24

8. "#define" versus "con

9. "+" in fopen() mode

10. fopen(fptr,"r+") example

11. "a+" fopen mode

12. testing for feof() and "rb+" filemode

 

 
Powered by phpBB® Forum Software