[comp.lang.c] Help needed with <fgets> and strings.

V053MDHL%UBVMS.BITNET@wiscvm.wisc.edu (03/27/87)

       I am writing a program that manipulates data inside a data file. One of
 the manipulations requires scanning the data strings (character) for the pres-
 ence of newline characters. I am using <fgets> in the program, but I can not
 get it to work. I am working on a Digital VAX. I have noticed a few points :

        [1]  The <fgets> function returns a pointer to the first character in
             the string. It was my belief that the pointer should point to the
             last <\0> character, as I already know where the string begins.

        [2]  In scanning the string for characters, I can compare with neither
             "\n" or "\0", as neither of these characters, while actually pre-
             sent in the strings, registers when compared.

       Is the compiler wrong? Any help in this would be greatly appreciated.

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

                     "Mutant Hunter"         V053MDHL@UBVMSC

amos@instable.UUCP (03/27/87)

In article <5902@brl-adm.ARPA> V053MDHL%UBVMS.BITNET@wiscvm.wisc.edu writes:
> ... I am working on a Digital VAX ...

This is not enough, there are at least 6 different O.S. on vaxen; I suspect
however that 'fgets' should behave the sames way on all.

>        [1]  The <fgets> function returns a pointer to the first character in
>             the string. It was my belief that the pointer should point to the
>             last <\0> character, as I already know where the string begins.

This is done so you can do things like strcmp("END", fgets(...))

>        [2]  In scanning the string for characters, I can compare with neither
>             "\n" or "\0", as neither of these characters, while actually pre-
>             sent in the strings, registers when compared.

What do you mean 'does not register when compared'? How are the comparisons
done? are you comparing to '\n' or '\0' (notice the *single* quotes)?

-- 
	Amos Shapir
National Semiconductor (Israel)
6 Maskit st. P.O.B. 3007, Herzlia 46104, Israel  Tel. (972)52-522261
amos%nsta@nsc.com {hplabs,pyramid,sun,decwrl} 34.48'E 32.10'N

chris@mimsy.UUCP (03/27/87)

In article <5902@brl-adm.ARPA> V053MDHL%UBVMS.BITNET@wiscvm.wisc.edu writes:
>[1]  The <fgets> function returns a pointer to the first character in
>     the string. It was my belief that the pointer should point to the
>     last <\0> character ....

fgets returns either its first argument or NULL.  Where did you
acquire this belief?

>[2]  In scanning the string for characters, I can compare with neither
>     "\n" or "\0", as neither of these characters, while actually pre-
>     sent in the strings, registers when compared.

From the way you phrased this, I suspect you have a test that looks
like this:

	if (c == "\n" || c == "\0")

If `c' is a character or integer value, this should elicit a warning
from the compiler, since "\n" and "\0" become pointer values.  Change
the test to

	if (c == '\n' || c == '\0')

'x' produces an integer constant; "string" produces a pointer to
an unnamed aggregate constant, type array N of char, where N is
strlen(string)+1.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris@mimsy.umd.edu