int get_lines(char *lines[])
{
int n = 0;
char buffer[80];
puts("Enter one line at time; enter a blank when done");
while ((n < MAXLINES) && (gets(buffer) !=0 ) && (buffer[0] != '\0'))
{
if ((lines[n] = malloc(strlen(b uffer)+1)) == NULL)
return -1;
strcpy (lines[n++], buffer);
}
return n;
}
the above code is from the book ' Teach yourself C '
it's for reading a row at a time, and if i just press enter without any
input, the loop terminates.
Question..
1. gets(buffer) !=0 --> what does this mean ?
as far as i know, 'gets' function returns NULL when error occurs,
so it means gets(buffer) !=NULL ?
either of them is alright ?
2. I'd like to change the while statement using fgets.
so i tried the below
while ((n < MAXLINES) && (fgets(buffer,8 0,stdin) !=0 ) &&
(buffer[0] != '\0'))
BUT !!
the loop doesn't end when i press enter without any input.
i guess (buffer[0] != '\0')) is wrong..
Thanks in advance..
BTW ,, i saw a group, comp.lang.c.mod erated,, what's the difference ?
{
int n = 0;
char buffer[80];
puts("Enter one line at time; enter a blank when done");
while ((n < MAXLINES) && (gets(buffer) !=0 ) && (buffer[0] != '\0'))
{
if ((lines[n] = malloc(strlen(b uffer)+1)) == NULL)
return -1;
strcpy (lines[n++], buffer);
}
return n;
}
the above code is from the book ' Teach yourself C '
it's for reading a row at a time, and if i just press enter without any
input, the loop terminates.
Question..
1. gets(buffer) !=0 --> what does this mean ?
as far as i know, 'gets' function returns NULL when error occurs,
so it means gets(buffer) !=NULL ?
either of them is alright ?
2. I'd like to change the while statement using fgets.
so i tried the below
while ((n < MAXLINES) && (fgets(buffer,8 0,stdin) !=0 ) &&
(buffer[0] != '\0'))
BUT !!
the loop doesn't end when i press enter without any input.
i guess (buffer[0] != '\0')) is wrong..
Thanks in advance..
BTW ,, i saw a group, comp.lang.c.mod erated,, what's the difference ?
Comment