Do we have any ways to tell the newline character in our os? If it is
not '\n', then Charles' fscanf statement have to be changed.
fscanf is for use with text mode streams, so the C library should do any
conversion required for files correct for that OS. But...
I mean if I have a text file which is gotten from other os in binary
mode, and I want a general program to deal with it, what should I do?
.... you are now talking about files from a foreign OS. If the file has
been imported as a binary copy, the C library on your machine has no
idea how to handle it. For the most common ones these days, you can
faitly easily write your own code to read a line, though. Mine uses an
algorithm something like:
if the character is CR
get the next character.
if that is not LF, unget it (push it back so it's got next time).
return end of line
else if the character is LF
get the next character.
if that is not CR, unget it (push it back so it's got next time).
return end of line
end if
That copes with lines ending with CR, LF, CRLF and LFCR, as long as
there are no stray CR or LF characters which are supposed to be part of
the data (which is silly but occasionally happens).
Of course, your foreign file might have come from a system where each
like is represented as a 2 byte count followed by that many characters
in the line, or where all files are kept in a compressed form, or text
files have a header stating the line width and all lines are constant
width, or something more strange, and there is no way that you can
automatically detect all of the possibilities...
Chris C