R
Ramesh
Hi friends,
I want to learn how to use EOF in C.Please help me.
I want to learn how to use EOF in C.Please help me.
Hi friends,
I want to learn how to use EOF in C.Please help me.
sumedh said:C provides u with EOF denoting end of file.
The following code may
help you.
int get_line(char line[], int max)
{
int nch = 0;
int ch;
max = max - 1; //initally max is pointing to \0
//So we need to get the char before \0
while((ch = getchar()) != EOF) //getchar returns char from
{
if(ch == '\n')
break;
if(nch < max)
{
line[nch] = ch ;
nch = nch + 1;
}
}
if(ch == EOF && nch == 0)
return EOF;
line[nch] = '\0';
return nch;
}
#include <stdio.h>
extern int get_line(char [], int);
main()
{
char l[200];
while(getline(l, 200) != EOF)
Ramesh said:I want to learn how to use EOF in C.Please help me.
max = max - 1;
while((ch = getchar()) != EOF) //getchar returns char from
{
if(ch == '\n')
break;
if(nch < max)
{
line[nch] = ch;
nch = nch + 1;
}
}
if(ch == EOF && nch == 0)
return EOF;
the function fgetc() will return an integer, not a character as you may haveRamesh said:Hi friends,
I want to learn how to use EOF in C.Please help me.
Malcolm said:the function fgetc() will return an integer, not a character as you may have
imagined, in the range 0-255 except some very odd systems that don't use 8
bit bytes. It returns -1 or EOF to indicate the end of input.
Malcolm McLean said:the function fgetc() will return an integer, not a character as you may have
imagined, in the range 0-255 except some very odd systems that don't use 8
bit bytes. It returns -1 or EOF to indicate the end of input.
In practise you always need this construct
int ch;
FILE *fp;
while( (ch = fgetc(fp)) != EOF)
Kelsey said:[snips]
max = max - 1;
If the user does something silly, such as passing in 0, or a negative
value, doesn't this end up doing bad things to your loop?
while((ch = getchar()) != EOF) //getchar returns char from
{
if(ch == '\n')
break;
if(nch < max)
{
line[nch] = ch ;
nch = nch + 1;
}
}
if(ch == EOF && nch == 0)
return EOF;
Hmm. So if we get EOF, we simply discard the data read to that point,
even though it may be perfectly good data?
Why not slap a \0 on the
sucker to turn it into a proper string?
Malcolm McLean said:the function fgetc() will return an integer, not a character as you
may have imagined, in the range 0-255 except some very odd systems
that don't use 8 bit bytes.
It returns -1 or EOF to indicate the end
of input. That's why it returns an integer instead of a char. Othewise
there would be one value that could not be represented.
An '\0' by itself is not a string.
santosh said:Kelsey Bjarnason wrote: [...]Why not slap a \0 on the
sucker to turn it into a proper string?
An '\0' by itself is not a string.
santosh said:Kelsey said:[snips]
max = max - 1;
If the user does something silly, such as passing in 0, or a negative
value, doesn't this end up doing bad things to your loop?
while((ch = getchar()) != EOF) //getchar returns char from
{
if(ch == '\n')
break;
if(nch < max)
{
line[nch] = ch ;
nch = nch + 1;
}
}
if(ch == EOF && nch == 0)
return EOF;
Hmm. So if we get EOF, we simply discard the data read to that point,
even though it may be perfectly good data?
Notice the other half of the expression? Anyway, regardless the code
is broken since if nch is not less than max, then additional input is
simply discarded.
Why not slap a \0 on the
sucker to turn it into a proper string?
An '\0' by itself is not a string.
Keith said:santosh said:Kelsey Bjarnason wrote: [...]Why not slap a \0 on the
sucker to turn it into a proper string?
An '\0' by itself is not a string.
A sequence consisting of a single '\0' character is a string,
specifically an empty string. (I'm ignoring whatever context led up
to this.)
(Just out of curiosity, how do you pronounce '\0' so that it's
"An '\0'" rather than "A '\0'"?)
Hi friends,
I want to learn how to use EOF in C.Please help me.
C provides u with EOF denoting end of file. The following code may
help you.
int get_line(char line[], int max)
{
int nch = 0;
int ch;
max = max - 1; //initally max is pointing to \0
//So we need to get the char before \0
while((ch = getchar()) != EOF) //getchar returns char from
{
if(ch == '\n')
break;
if(nch < max)
{
line[nch] = ch ;
nch = nch + 1;
}
}
if(ch == EOF && nch == 0)
return EOF;
line[nch] = '\0';
return nch;
}
#include <stdio.h>
extern int get_line(char [], int);
main()
{
char l[200];
while(getline(l, 200) != EOF)
printf("line read\"%s\"\n", l);
return 0;
}
"An ought". Or maybe "An ul". I've even heard "An ee-oh-ess".Keith Thompson said:santosh said:Kelsey Bjarnason wrote: [...]Why not slap a \0 on the
sucker to turn it into a proper string?
An '\0' by itself is not a string.
A sequence consisting of a single '\0' character is a string,
specifically an empty string. (I'm ignoring whatever context led up
to this.)
(Just out of curiosity, how do you pronounce '\0' so that it's
"An '\0'" rather than "A '\0'"?)
A string of no sausages is a very different thing to no string of sausages.Richard Heathfield said:The Standard mentions "empty strings" on several occasions. An empty
string is still a string.
Malcolm McLean said:A string of no sausages is a very different thing to no string of
sausages.
There may be no sausages on it, but the string is still there.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.