Re: confused abt file operations



>If I open a binary file in text mode and use text functions to
>read it then will I be reading numbers as characters or actual
>values?
>What if I open a text file and read it using binary read functions.

The difference between a binary and text file on most implementations
is either (a) none at all, or (b) the line ending (\n vs. \r\n vs.
\n\r vs. something else). This doesn't rule out things like binary
line numbers and line lengths preceeding the line with no end-of-line
terminator, but these are very rare.

What is a "text function"? There is no such division ("fread and
fwrite are for binary, printf, fgets, fputs, scanf, etc. are for
text" is a myth).

The practical effect is likely that you will see stray \r characters
before or after \n characers if you read a text file as binary. If
you read a binary file as text, some of the \r characters might
vanish. You might see a binary file read as text somewhat shorter
than expected if the binary file contains a text "end of file
character" which is interpreted as such.

Gordon L. Burditt
.



Relevant Pages

  • Re: fseek
    ... >>a text stream to a binary file or vice versa. ... >>binary file in text mode or vice versa results in undefined behavior. ... newline characters are treated the same as ... On IBM's mainframe OSs lines never have newline characters on them, ...
    (comp.lang.c)
  • Re: Replacing characters in file
    ... Well, if it is not a text file but a binary file, then it isn't very ... meaningful to talk about characters. ... Computers are notorious at being extremely pedantic. ... It doesn't work in SED because SED aborts when it hits the first EOF ...
    (comp.lang.perl.misc)
  • Re: Replacing characters in file
    ... Well, if it is not a text file but a binary file, then it isn't very ... meaningful to talk about characters. ... It doesn't work in SED because SED aborts when it hits the first EOF ... downloading Active Perl and see what happens there, ...
    (comp.lang.perl.misc)
  • Re: Delete key leaves ghost characters at command prompt in xterm
    ... incorrect characters, and output characters to be garbled. ... remember how xterm behaved in those circumstances. ... But I am wondering if I might have cat a binary file by ... page (the pager used by man usually is configured to reset the display ...
    (comp.os.linux.misc)
  • Re: replacing text data in a binary file
    ... &HFF for the byte. ... That explains the cbyte in ... >> If you are editing a binary file I would recommend opening the file with ... >> removing or adding characters may actually corrupt the file, ...
    (microsoft.public.dotnet.languages.vb)