Hi friends,
I want to learn how to use EOF in C.Please help me. 29 49682
On Jul 11, 10:41 am, Ramesh <chary...@gmail.comwrote:
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;
}
sumedh wrote:
On Jul 11, 10:41 am, Ramesh <chary...@gmail.comwrote:
Hi friends,
I want to learn how to use EOF in C.Please help me.
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?
printf("line read\"%s\"\n", l);
return 0;
}
Ramesh <ch******@gmail.comwrites:
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.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[snips]
On Tue, 10 Jul 2007 22:51:21 -0700, sumedh wrote:
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?
"Ramesh" <ch******@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
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);
}
--
Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm
Malcolm McLean wrote:
"Ramesh" <ch******@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
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.
EOF needn't equal -1.
In article <pt******************************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:
>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
>int ch;
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.
>{
/* process characters here */
printf("val %d char %c\n", ch, ch); }
--
Programming is what happens while you're busy making other plans.
Kelsey Bjarnason wrote:
[snips]
On Tue, 10 Jul 2007 22:51:21 -0700, sumedh wrote:
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.
"Malcolm McLean" <re*******@btinternet.comwrites:
"Ramesh" <ch******@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
>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 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]
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
santosh said:
<snip>
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.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
santosh <sa*********@gmail.comwrites:
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'"?)
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
santosh <sa*********@gmail.comwrites:
Kelsey Bjarnason wrote:
>[snips]
On Tue, 10 Jul 2007 22:51:21 -0700, sumedh wrote:
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.
Keith Thompson wrote:
santosh <sa*********@gmail.comwrites:
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.
On 11 Jul, 06:51, sumedh <excuseme2...@gmail.comwrote:
On Jul 11, 10:41 am, Ramesh <chary...@gmail.comwrote:
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.
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
santosh <sa*********@gmail.comwrites:
>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".
--
Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
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.
--
Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm
"Malcolm McLean" <re*******@btinternet.comwrites:
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
>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.
There may be no sausages on it, but the string is still there.
Richard wrote, On 12/07/07 10:57:
"Malcolm McLean" <re*******@btinternet.comwrites:
>"Richard Heathfield" <rj*@see.sig.invalidwrote in message
>>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.
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 */
--
Flash Gordon
On Wed, 11 Jul 2007 21:39:58 +0000, santosh wrote:
Kelsey Bjarnason wrote:
Notice the other half of the expression?
Grr. Damn this being human thing sucks. :)
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.
Pass it to strlen, it'll work. Pass the results of whatever you've got
instead, it'll scream and die - if you're lucky.
Flash Gordon said:
<snip>
The standard even provides methods of expressing these things Malcolm
acknowledges as different.
char *sausages = NULL; /* No string */
char *sausages == ""; /* Empty string */
/* ITYM syntax error */
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Kelsey Bjarnason <kb********@gmail.comwrites:
On Wed, 11 Jul 2007 21:39:58 +0000, santosh wrote:
[...]
>An '\0' by itself is not a string.
Pass it to strlen, it'll work.
strlen('\0') invokes undefined behavior.
I know that's not what you meant. If you pass the address of a
character with value '\0' to strlen(), it will return 0.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Richard Heathfield wrote, On 12/07/07 21:10:
Flash Gordon said:
<snip>
>The standard even provides methods of expressing these things Malcolm acknowledges as different.
char *sausages = NULL; /* No string */ char *sausages == ""; /* Empty string */
/* ITYM syntax error */
s/==/=/
--
Flash Gordon
"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message
news:rj************@news.flash-gordon.me.uk...
Richard Heathfield wrote, On 12/07/07 21:10:
>Flash Gordon said:
<snip>
>>The standard even provides methods of expressing these things Malcolm acknowledges as different.
char *sausages = NULL; /* No string */ char *sausages == ""; /* Empty string */
/* ITYM syntax error */
s/==/=/
--
Flash Gordon
char O=0; /* sausage */
char *sausages = 0-0-0-0-0-0-0; /* no string */
char *sausage = &O; /* empty string */
--
Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm
"Malcolm McLean" <re*******@btinternet.comwrites:
char O=0; /* sausage */
char *sausages = 0-0-0-0-0-0-0; /* no string */
char *sausage = &O; /* empty string */
char *confusing_sausage = &O-0-0-0-0-0-0-0; /* empty string */
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
"Malcolm McLean" <re*******@btinternet.comwrote:
char O=0; /* sausage */
char *sausages = 0-0-0-0-0-0-0; /* no string */
char *sausage = &O; /* empty string */
# Constraints
# 1 The operand of the unary & operator shall be either a function
# designator, the result of a [] or unary * operator, or an lvalue
# that designates an object that is not a bit-field and is not
# declared with the register storage-class specifier.
A literal 0 is not any of those, so &0 violates this constraint. Did you
perhaps mean "0" (or even &"0")?
Richard rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"Malcolm McLean" <re*******@btinternet.comwrote:
>char O=0; /* sausage */
char *sausages = 0-0-0-0-0-0-0; /* no string */ char *sausage = &O; /* empty string */
# Constraints
# 1 The operand of the unary & operator shall be either a function
# designator, the result of a [] or unary * operator, or an lvalue
# that designates an object that is not a bit-field and is not
# declared with the register storage-class specifier.
A literal 0 is not any of those, so &0 violates this constraint. Did you
perhaps mean "0" (or even &"0")?
Get a better font, or copy-and-paste the code and try to compile it.
The initializer for 'sausage' is '&O', not '&0' (the letter, not the
digit); 'O' was declared three lines earlier as an object of type
char, initialized to zero.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
On Thu, 12 Jul 2007 13:59:20 -0700, Keith Thompson wrote:
Kelsey Bjarnason <kb********@gmail.comwrites:
>On Wed, 11 Jul 2007 21:39:58 +0000, santosh wrote:
[...]
>>An '\0' by itself is not a string.
Pass it to strlen, it'll work.
strlen('\0') invokes undefined behavior.
True.
I know that's not what you meant. If you pass the address of a
character with value '\0' to strlen(), it will return 0.
In the context used. '\0' "works". In the general case, it doesn't, for
hopefully obvious reasons.
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
>"Malcolm McLean" <re*******@btinternet.comwrote:
>>char O=0; /* sausage */
char *sausages = 0-0-0-0-0-0-0; /* no string */ char *sausage = &O; /* empty string */
# Constraints # 1 The operand of the unary & operator shall be either a function # designator, the result of a [] or unary * operator, or an lvalue # that designates an object that is not a bit-field and is not # declared with the register storage-class specifier.
A literal 0 is not any of those, so &0 violates this constraint. Did you perhaps mean "0" (or even &"0")?
Get a better font, or copy-and-paste the code and try to compile it.
The initializer for 'sausage' is '&O', not '&0' (the letter, not the
digit); 'O' was declared three lines earlier as an object of type
char, initialized to zero.
It was a charred sausage.
--
Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm
"Malcolm McLean" <re*******@btinternet.comwrote:
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"Malcolm McLean" <re*******@btinternet.comwrote: char O=0; /* sausage */
char *sausages = 0-0-0-0-0-0-0; /* no string */ char *sausage = &O; /* empty string */
# Constraints
# 1 The operand of the unary & operator shall be either a function
# designator, the result of a [] or unary * operator, or an lvalue
# that designates an object that is not a bit-field and is not
# declared with the register storage-class specifier.
A literal 0 is not any of those, so &0 violates this constraint. Did you
perhaps mean "0" (or even &"0")?
Get a better font, or copy-and-paste the code and try to compile it.
The initializer for 'sausage' is '&O', not '&0' (the letter, not the
digit); 'O' was declared three lines earlier as an object of type
char, initialized to zero.
*Hangs head in shame*
It was a charred sausage.
*Groans*
Richard This discussion thread is closed Replies have been disabled for this discussion. Similar topics
3 posts
views
Thread by William C. White |
last post: by
|
2 posts
views
Thread by Albert Ahtenberg |
last post: by
|
3 posts
views
Thread by James |
last post: by
|
reply
views
Thread by Ollivier Robert |
last post: by
|
1 post
views
Thread by Richard Galli |
last post: by
|
4 posts
views
Thread by Albert Ahtenberg |
last post: by
|
1 post
views
Thread by inderjit S Gabrie |
last post: by
|
2 posts
views
Thread by Jack |
last post: by
|
3 posts
views
Thread by Sandwick |
last post: by
| | | | | | | | | | |