Mail Archives: djgpp/2001/09/24/03:00:13
Gwen wrote:
>
> > I'm experiencing some problems with stdio.h and I want to use iostream.h,
> In fact, I think I'll have the same problems with iostream.h : I can't
> recognize when I read a file when there is a carriage return.
> I tried this :
>
> int c=getc(f);
> while (c!=13) { addch(c); }
>
> It displays all the characteres of my files, with the cariage return but it
> doesn't break the loop and it reachs the end of file and loops forever.
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").
Besides, you should always be very defensive in input loops like that;
at the
very least, you should break out of the loop if you reach EOF. I think
the
example above also omits the 'c = getc(f)' inside the loop body.
- Raw text -