Mail Archives: djgpp/2001/09/24/07:32:52
Tim Van Holder <tim DOT vanholder AT falconsoft DOT be> wrote in
news:3BAED94A DOT E957ABFD AT falconsoft DOT be:
> The main problem is that for a file opened in text mode (the default),
> CR/LF pairs are read as a signle LF. So if it's a text file you're
> reading, there's a good chance you'll never see a CR. Try using
> fopen(file, "rb") instead of fopen(file, "r").
I don't think that is correct advice. The purpose of opening text mode is
to transparently treat different line endings as '\n'. It does not change
the meaning of a stand-alone CR.
The problem with the posted snippet was that it went into an infinite loop
if the first character read from the stream was not CR, it went into an
infinite loop. Assmuing the OP wanted to get rid of line-endings, not the
byte 0x0d, he should open the file in text mode, and use code similar to:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int c;
FILE *fin;
fin = fopen(argv[1], "rt");
if( fin == NULL )
{
perror(argv[1]);
exit(EXIT_FAILURE);
}
while( (c = getc(fin)) != EOF)
if( c != '\n' ) putc(c, stdout);
return EXIT_SUCCESS;
}
Try this code on files with Unix and DOS line endings, and you'll see the
same behavior.
Sinan.
--
--------------------------------
A. Sinan Unur
http://www.unur.com/
- Raw text -