How to use EOF in C

S

sumedh

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;
}
 
S

santosh

sumedh said:
C provides u with EOF denoting end of file.

To be precise, EOF is a macro defined in stdio.h that yields an
integer constant. It is used as the return value by many of C's I/O
functions when they encounter an end-of-file condition or an error.
The pair of functions feof and ferror are typically used to find out
which of the two it is.
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

This type of comment is only supported as of C99. Also it tends to
break when used in posts to Usenet.
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()

Declare main to explicitly return an int. And if you don't intend to
access the command line you can use void to indicate that it does not
take any parameters.

int main(void) {
{
char l[200];

while(getline(l, 200) != EOF)

Where is getline defined?
 
K

Keith Thompson

Ramesh said:
I want to learn how to use EOF in C.Please help me.

Any decent C textbook will explain this. I recommend Kernighan &
Ritchie's _The C Programming Language_, 2nd Edition, commonly known as
K&R2.

But I think you're asking the wrong question. Rather than asking how
to use EOF, you should probably be trying to learn how to read and
process input. The use of EOF is just a part of that.
 
K

Kelsey Bjarnason

[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?
 
M

Malcolm McLean

Ramesh said:
Hi friends,
I want to learn how to use EOF in C.Please help me.
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.

In practise you always need this construct

int ch;
FILE *fp;

while( (ch = fgetc(fp)) != EOF)
{
/* process characters here */
printf("val %d char %c\n", ch, ch);
}
 
S

santosh

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.

EOF needn't equal -1.
 
W

Walter Roberson

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.

To be more precise, it returns some specific (implementation-
defined) negative value to indicate the end of input, and the
particular implementation negative value is available by using
the macro EOF -- no matter what -particular- value the implementation
uses, if you refer to EOF then you will get the right value for
that implementation. But the implementation value is not
necessarily -1, so do not hard-code comparisons to -1. You
could code checks for < 0 if you wanted though, as the only
negative value allowed to be returned by fgetc() is whatever
the system is using for EOF.

In practise you always need this construct

Annotating slightly for the OP: notice Malcolm used 'int'
as the type, not 'char'.
FILE *fp;
while( (ch = fgetc(fp)) != EOF)

Another annotation for the OP: notice that the assignment
to ch is within () . If you were to leave out the () and code

while ( ch = fgetc(fp) != EOF ) /* WRONG */

then this would be wrong because C would parse this as

while ( ch = (fgetc(fp) != EOF) )

that is, ch would get assigned the result of the -comparison-
rather than the character that was read in.
 
S

santosh

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.
 
K

Keith Thompson

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 an int (one of several integer types), not a char (also one
of several integer types, as well as being one of several character
types).

"int" and "integer" mean very different things, as do "char" and
"character".
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.

EOF is typically defined as (-1), but the only requirement is that
it's of type int with a negative value. I actually don't know of any
implementation where EOF has a value other than -1; nevertheless I
would consider something like

while( (ch = fgetc(fp)) != -1) /* ... */

to be badly broken (even if it happens to work).

[snip]
 
R

Richard Heathfield

santosh said:

An '\0' by itself is not a string.

The Standard says: "A string is a contiguous sequence of characters
terminated by and including the first null character."

The Standard mentions "empty strings" on several occasions. An empty
string is still a string.
 
K

Keith Thompson

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'"?)
 
R

Richard

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.

Of course it is.

If you have a character pointer pointing to that then the pointer is
still pointing to a character string.
 
S

santosh

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.)

Okay, you're correct, (also Richard Heathfield and Richard). I must
consult the Standard more before posting.
(Just out of curiosity, how do you pronounce '\0' so that it's
"An '\0'" rather than "A '\0'"?)

I don't. It's a mistake obviously.
 
S

Spiros Bousbouras

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;

}


This will not compile for 2 reasons:
1) You define get_line but you call getline
(mentioned already).
2) You use EOF before including <stdio.h>
If I remember correctly macros are defined from
the point they appear until the end of file.

With the obvious corrections the code would compile,
work and shows some sort of usage for EOF. Others
have commented that it truncates the line read but
that doesn't mean that it's broken. On several occasions
I've needed the first n characters of each line of a file
for fixed n.
 
M

Malcolm McLean

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'"?)
"An ought". Or maybe "An ul". I've even heard "An ee-oh-ess".
 
M

Malcolm McLean

Richard Heathfield said:
The Standard mentions "empty strings" on several occasions. An empty
string is still a string.
A string of no sausages is a very different thing to no string of sausages.
 
R

Richard

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.
 
F

Flash Gordon

Richard wrote, On 12/07/07 10:57:
There may be no sausages on it, but the string is still there.

The standard even provides methods of expressing these things Malcolm
acknowledges as different.

char *sausages = NULL; /* No string */
char *sausages == ""; /* Empty string */
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top