Newbie question about fgets() Function 
Author Message
 Newbie question about fgets() Function

A newbie question:

The way I _want_ fgets() to work is for it to read until n-1 characters
are transfered to the input buffer or until it encounters a newline
(i.e., CR-LF), in which case it captures the newline.  I'm happy to
replace the CR with a NULL to form a proper string.

The way it _is_ working is that it reads the stream until it sees the
newline, but then places (get ready for it) a LF-NULL combination in the
buffer.  That's right, a hex 0D-00.  Huh?

By the way, this is Borland C++ v4.5.

Anybody know what's going on?  E-mail, please. TIA.



Tue, 12 May 1998 03:00:00 GMT  
 Newbie question about fgets() Function

Quote:

>A newbie question:

>The way I _want_ fgets() to work is for it to read until n-1 characters
>are transfered to the input buffer or until it encounters a newline
>(i.e., CR-LF), in which case it captures the newline.  I'm happy to
>replace the CR with a NULL to form a proper string.

>The way it _is_ working is that it reads the stream until it sees the
>newline, but then places (get ready for it) a LF-NULL combination in the
>buffer.  That's right, a hex 0D-00.  Huh?

Yes, this is the correct operation.  Within C, a newline (\n) is a LF alone.
If a stream is opened in text mode, the C I/O  library _will_ convert the C
newline char to the operating system's representation, on output, and from the
operating system's representation to \n on input.  If you _really_ want to see
the CR/LF chars that DOS uses, you have to open the stream in binary mode.

Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight


TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html



Tue, 12 May 1998 03:00:00 GMT  
 Newbie question about fgets() Function


Quote:
>A newbie question:

>The way I _want_ fgets() to work is for it to read until n-1 characters
>are transfered to the input buffer or until it encounters a newline
>(i.e., CR-LF), in which case it captures the newline.  I'm happy to
>replace the CR with a NULL to form a proper string.

Within C lines are by definition terminated with a linefeed (ASCII value 10)
character. If the file is opened in text more the library will convert
automatically between the system's line convention and this convention.
Thus if you are using a system that uses CR-LF to terminate a line the
library will convert all instances of these to plain LF with input from
a file opened in text mode.

Quote:
>The way it _is_ working is that it reads the stream until it sees the
>newline, but then places (get ready for it) a LF-NULL combination in the
>buffer.  That's right, a hex 0D-00.  Huh?

This looks confused. LF is a linefeed with value 10 (0x0a) in ASCII, CR is
a carriage return with ASCII value 13 (0x0d).

What do you actually see?

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


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



Tue, 12 May 1998 03:00:00 GMT  
 Newbie question about fgets() Function

Quote:
> Within C lines are by definition terminated with a linefeed (ASCII
> value 10) character.

He means, of course:

  ... terminated with a linefeed character (value 10 if the execution
  character set is ASCII).

The execution character set is implementation-defined, though the
standard does impose some requirements on it.
--
Mark Brader            | "I don't care HOW you format   char c; while ((c =

SoftQuad Inc., Toronto |  bug waiting to happen from the outset." --Doug Gwyn

My text in this article is in the public domain.



Thu, 14 May 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. My own fgets function?? and functions in general

2. Newbie: EOF test with fgets

3. newbie: about fgets(), getchar() and getc()

4. NEWBIE fgets PROBLEM

5. annoying fgets problem <fgets>

6. newbie question: returning structs from functions

7. Newbie Function question

8. Newbie question about function to get float

9. Newbie Question: select function

10. Newbie question: malloc() function

11. another newbie question: strncpy function

12. Newbie question: Pointer to an array and passing it into a function

 

 
Powered by phpBB® Forum Software