Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > feof

Reply
 
 
Joriveek
Guest
Posts: n/a
 
      09-13-2005
Hi,

If I use while(!feof( FileRead )), it reads each line from my input file
until it encounters an empty space or so,

but when I have empty spaces within one line, it is not working, it is only
reading until it finds the first empty space. But I want to read all line
and then go to next line when the characters start from \n (second line),

hw can i read this?

Thanks
J.


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      09-13-2005

"Joriveek" <(E-Mail Removed)> wrote in message
news:dg6q3c$9f6$(E-Mail Removed)...
> Hi,
>
> If I use while(!feof( FileRead )), it reads each line from my input file
> until it encounters an empty space or so,


No, 'feof()' does not read anything. It simply tells you
whether the last input request for the stream denoted by
'FileRead' tried to read *past* end the of the file.

>
> but when I have empty spaces within one line, it is not working, it is
> only reading until it finds the first empty space. But I want to read all
> line and then go to next line when the characters start from \n (second
> line),
>
> hw can i read this?


Use 'fgets()'. Be sure to test its return value before
proceeding with further i/o on the stream (See its
documentation for the possible return values and their
meanings).

Also see the C FAQ for why you should not test for EOF
before reading, but *after*.
http://www.eskimo.com/~scs/C-faq/faq.html
(See section 12)

-Mike

>



 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      09-13-2005
Joriveek wrote on 13/09/05 :
> If I use while(!feof( FileRead )), it reads each line from my input file
> until it encounters an empty space or so,


Non. It will probably acts like an infinite loop, unless you provide a
read function. But even in this case, the behaviour is wrong, because
feof() has a meaning only after an end of reading has occur. feof() and
ferror() can be used (rarely, actually) to determine the cause of the
stop.

> but when I have empty spaces within one line, it is not working, it is only
> reading until it finds the first empty space. But I want to read all line and
> then go to next line when the characters start from \n (second line),
>
> hw can i read this?


Read the FAQ, please.

http://www.eskimo.com/~scs/C-faq/faq.html

You should use a reading I/O function (fgetc(), fgets(), fread() etc.)
and test the return value to detect the end of the reading.


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"


 
Reply With Quote
 
Joriveek
Guest
Posts: n/a
 
      09-14-2005
Thanks fgets() is of a great help, but it is only reading first line from my
input file, what should I make this to read all other lines?

Thanks
J.


"Emmanuel Delahaye" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Joriveek wrote on 13/09/05 :
>> If I use while(!feof( FileRead )), it reads each line from my input file
>> until it encounters an empty space or so,

>
> Non. It will probably acts like an infinite loop, unless you provide a
> read function. But even in this case, the behaviour is wrong, because
> feof() has a meaning only after an end of reading has occur. feof() and
> ferror() can be used (rarely, actually) to determine the cause of the
> stop.
>
>> but when I have empty spaces within one line, it is not working, it is
>> only reading until it finds the first empty space. But I want to read all
>> line and then go to next line when the characters start from \n (second
>> line),
>>
>> hw can i read this?

>
> Read the FAQ, please.
>
> http://www.eskimo.com/~scs/C-faq/faq.html
>
> You should use a reading I/O function (fgetc(), fgets(), fread() etc.) and
> test the return value to detect the end of the reading.
>
>
> --
> Emmanuel
> The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
> The C-library: http://www.dinkumware.com/refxc.html
>
> I once asked an expert COBOL programmer, how to
> declare local variables in COBOL, the reply was:
> "what is a local variable?"
>
>



 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      09-14-2005
Joriveek wrote:

> Thanks fgets() is of a great help, but it is only reading first line from
> my input file, what should I make this to read all other lines?


Call it again?

[It's very hard to see what you're doing wrong, if we can't see what
you're doing at all.]

--
Chris "electric hedgehog" Dollin
Logic is rational /re/construction. Life is construction.
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-14-2005
"Joriveek" <(E-Mail Removed)> wrote:

Please don't top-post. Fixed.

>"Emmanuel Delahaye" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>> Joriveek wrote on 13/09/05 :
>>> If I use while(!feof( FileRead )), it reads each line from my input file
>>> until it encounters an empty space or so,

>>
>> Non. It will probably acts like an infinite loop, unless you provide a
>> read function. But even in this case, the behaviour is wrong, because
>> feof() has a meaning only after an end of reading has occur. feof() and
>> ferror() can be used (rarely, actually) to determine the cause of the
>> stop.

<snip>
>> Read the FAQ, please.
>>
>> http://www.eskimo.com/~scs/C-faq/faq.html


Did you read the FAQ already?

>> You should use a reading I/O function (fgetc(), fgets(), fread() etc.) and
>> test the return value to detect the end of the reading.
>>

>Thanks fgets() is of a great help, but it is only reading first line from my
>input file, what should I make this to read all other lines?


No, you didn't read the FAQ. Sigh.

General answer:
get yourself a decent textbook on C, I recommend "The C Programming
Language" by Brian W. Kernighan and Dennis M. Ritchie, 2nd edition

Specific answer:
use a loop

Oh, and read the FAQ, please.

Best regards
--
Irrwahn Grausewitz ((E-Mail Removed))
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
 
Reply With Quote
 
Joriveek
Guest
Posts: n/a
 
      09-14-2005
Thanks I sorted out


"Irrwahn Grausewitz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Joriveek" <(E-Mail Removed)> wrote:
>
> Please don't top-post. Fixed.
>
>>"Emmanuel Delahaye" <(E-Mail Removed)> wrote in message
>>news:(E-Mail Removed). ..
>>> Joriveek wrote on 13/09/05 :
>>>> If I use while(!feof( FileRead )), it reads each line from my input
>>>> file
>>>> until it encounters an empty space or so,
>>>
>>> Non. It will probably acts like an infinite loop, unless you provide a
>>> read function. But even in this case, the behaviour is wrong, because
>>> feof() has a meaning only after an end of reading has occur. feof() and
>>> ferror() can be used (rarely, actually) to determine the cause of the
>>> stop.

> <snip>
>>> Read the FAQ, please.
>>>
>>> http://www.eskimo.com/~scs/C-faq/faq.html

>
> Did you read the FAQ already?
>
>>> You should use a reading I/O function (fgetc(), fgets(), fread() etc.)
>>> and
>>> test the return value to detect the end of the reading.
>>>

>>Thanks fgets() is of a great help, but it is only reading first line from
>>my
>>input file, what should I make this to read all other lines?

>
> No, you didn't read the FAQ. Sigh.
>
> General answer:
> get yourself a decent textbook on C, I recommend "The C Programming
> Language" by Brian W. Kernighan and Dennis M. Ritchie, 2nd edition
>
> Specific answer:
> use a loop
>
> Oh, and read the FAQ, please.
>
> Best regards
> --
> Irrwahn Grausewitz ((E-Mail Removed))
> welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
> clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
> clc frequent answers: http://benpfaff.org/writings/clc



 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      09-14-2005
Joriveek wrote on 14/09/05 :
> Thanks fgets() is of a great help, but it is only reading first line from my
> input file, what should I make this to read all other lines?


Consider a code structure like while()...

Time to open a C-book...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-14-2005
"Joriveek" <(E-Mail Removed)> writes:
> Thanks I sorted out


Great.

Again, please don't top-post. Your response belongs below any quoted
text, and you should trim anything that isn't relevant. The idea is
that each followup should be readable by itself, from top to bottom.
See nearly any article this newsgroup for an example.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
feof(), fseek(), fread() ericunfuk C Programming 20 03-27-2007 09:22 PM
A question about fscanf and feof ! ehui928 C Programming 7 11-07-2006 01:48 AM
EOF vs. feof() and ferror() rCs C Programming 8 11-01-2006 04:45 PM
question about gets() and feof(). sunnyboyGuo@gmail.com C Programming 3 09-16-2005 05:36 PM
feof usage Mantorok Redgormor C Programming 28 09-29-2003 04:10 AM



Advertisments