[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Determining EOF using fseek()?

Orion

6/29/2011 9:36:00 PM

Hey,

I was wondering if it was possible to determine if you hit 'EOF' using
fseek? I'm using fseek to traverse through the file from start to end
and capturing the data into a linked list structure. However, my loop
doesn't seem to work well - it totally fumbles out actually:

while ((a = fseek(fp,0,SEEK_CUR)) == 0){
// code here

}

Its quite important for me not to disrupt the current position of the
cursor since I rely on that to fetch the data from the text file. I
thought that the loop would work fine since fseek only returns a
non-zero integer on an error but unfortunately this is not the case.
Anyone with suggestions with using fseek() or some other function?

Any help would be greatly appreciated! Thanks.
6 Answers

John Gordon

6/29/2011 9:46:00 PM

0

> Anyone with suggestions with using fseek() or some other function?

If you just want to test for end-of-file, use feof().

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Eric Sosman

6/30/2011 1:51:00 AM

0

On 6/29/2011 5:36 PM, Orion wrote:
> Hey,
>
> I was wondering if it was possible to determine if you hit 'EOF' using
> fseek? I'm using fseek to traverse through the file from start to end
> and capturing the data into a linked list structure.

"Traverse through the file from start to end" -- sounds like plain
sequential input operations like fread() and getc() would do the job
just fine. From what you've said, I can't see a need for fseek().

> However, my loop
> doesn't seem to work well - it totally fumbles out actually:
>
> while ((a = fseek(fp,0,SEEK_CUR)) == 0){
> // code here
>
> }

The fseek() here looks a lot like a no-op: "Go to the position
zero bytes distant from the current position (i.e., go to Right Here),
and tell me if you couldn't get there." What were you expecting it
to do? And why?

> Its quite important for me not to disrupt the current position of the
> cursor since I rely on that to fetch the data from the text file. I
> thought that the loop would work fine since fseek only returns a
> non-zero integer on an error but unfortunately this is not the case.

What do you mean, "This is not the case?" Did you get a non-
zero value from your no-op fseek()? If so, what steps did you take
to determine that there was no error?

> Anyone with suggestions with using fseek() or some other function?
>
> Any help would be greatly appreciated! Thanks.

I think you need to describe your intent better. Exhibiting
one no-op fseek() call doesn't give us much to go on.

--
Eric Sosman
esosman@ieee-dot-org.invalid

John Gordon

6/30/2011 3:55:00 AM

0

In <iugksq$4ev$1@dont-email.me> Eric Sosman <esosman@ieee-dot-org.invalid> writes:

> > while ((a = fseek(fp,0,SEEK_CUR)) == 0){

> The fseek() here looks a lot like a no-op: "Go to the position
> zero bytes distant from the current position (i.e., go to Right Here),
> and tell me if you couldn't get there." What were you expecting it
> to do? And why?

I think he's trying to use it as a roundabout substitute for feof().

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

David Thompson

7/11/2011 4:26:00 AM

0

On Wed, 29 Jun 2011 21:36:21 +0000 (UTC), Orion <won@bigpond.net.au>
wrote:

> Hey,
>
> I was wondering if it was possible to determine if you hit 'EOF' using
> fseek? I'm using fseek to traverse through the file from start to end
> and capturing the data into a linked list structure. However, my loop

I assume you are also reading because fseek can't 'capture ... data'.
And I guess you are skipping at least sometimes between reads, because
only then do you need fseek(); all C read (and write) operations
already advance the fileposition themselves.

> doesn't seem to work well - it totally fumbles out actually:
>
> while ((a = fseek(fp,0,SEEK_CUR)) == 0){
> // code here
>
> }
>
Nope. C doesn't require fseek() to detect if the new position, here
unchanged from old, is at or past EOF; and Unix (long the most
important platform for C implementation, though not the only one)
requires the opposite. In Unix it's entirely legal to fseek, or lseek,
beyond the current EOF, and if the file is writable and you write at
that position the file is extended to reach the point where you wrote,
with intervening zeros or as-if. Some filesystems optimize this case
by not actually storing (thus not actually zeroing) 'skipped' regions.
The seek fails only if the target position is not even representable.

C (and Unix) I/O doesn't 'look ahead' the way for example Pascal does.
A read operation that takes the last data before EOF succeeds. And in
some cases, more data may be added before you attempt the next read,
so the next read also succeeds. Only when you try to read and it
actually gets no data for *getc *gets *scanf etc. or less than
requested for fread does C treat that as EOF, and set feof().

And of course many (C) files aren't disk files. It's *impossible* to
know if you're 'about to EOF' on a terminal, a pipe, a socket, or a
magtape (not that anyone uses those anymore).

> Its quite important for me not to disrupt the current position of the
> cursor since I rely on that to fetch the data from the text file. I
> thought that the loop would work fine since fseek only returns a
> non-zero integer on an error but unfortunately this is not the case.
> Anyone with suggestions with using fseek() or some other function?
>
> Any help would be greatly appreciated! Thanks.

In standard C you have to try the input operation and *then* deal with
a return value that indicates EOF (or error).

If you want to limit yourself to disk files on bag-o-bytes systems --
which is just about all the ones people care about today -- you could
get the EOF value, either once at the begining if you assume or
enforce that no other process can append to the file and otherwise at
each check, and compare. The only 'standard' way is the fseek(,,END)
ftell() method -- and even that isn't officially guaranteed to work on
a binary file or to be comparable on a text file, but on a bag-o-bytes
implementation not to work the obvious way would be crazy. The
nonstandard way is stat() on Unix or its equivalent elsewhere.

But note you still need to deal with errors, which may occur on a read
operation even after you have correctly determined you are not at EOF,
so this actually makes your logic more complicated not less.

Squeamizh

7/11/2011 6:40:00 AM

0

On Jun 29, 2:36 pm, Orion <w...@bigpond.net.au> wrote:
> Hey,
>
> I was wondering if it was possible to determine if you hit 'EOF' using
> fseek? I'm using fseek to traverse through the file from start to end
> and capturing the data into a linked list structure. However, my loop
> doesn't seem to work well - it totally fumbles out actually:
>
> while ((a = fseek(fp,0,SEEK_CUR)) == 0){
>         // code here
>
> }
>
> Its quite important for me not to disrupt the current position of the
> cursor since I rely on that to fetch the data from the text file. I
> thought that the loop would work fine since fseek only returns a
> non-zero integer on an error but unfortunately this is not the case.
> Anyone with suggestions with using fseek() or some other function?
>
> Any help would be greatly appreciated! Thanks.

I guess you didn't like the answers you got in '04!
http://groups.google.com/group/comp.lang.c/browse_frm/thread/8a2feab451c13f24/5cf24c...

Uno

7/17/2011 6:21:00 AM

0

On 7/10/2011 11:40 PM, Squeamizh wrote:
> On Jun 29, 2:36 pm, Orion<w...@bigpond.net.au> wrote:
>> Hey,
>>
>> I was wondering if it was possible to determine if you hit 'EOF' using
>> fseek? I'm using fseek to traverse through the file from start to end
>> and capturing the data into a linked list structure. However, my loop
>> doesn't seem to work well - it totally fumbles out actually:
>>
>> while ((a = fseek(fp,0,SEEK_CUR)) == 0){
>> // code here
>>
>> }
>>
>> Its quite important for me not to disrupt the current position of the
>> cursor since I rely on that to fetch the data from the text file. I
>> thought that the loop would work fine since fseek only returns a
>> non-zero integer on an error but unfortunately this is not the case.
>> Anyone with suggestions with using fseek() or some other function?
>>
>> Any help would be greatly appreciated! Thanks.
>
> I guess you didn't like the answers you got in '04!
> http://groups.google.com/group/comp.lang.c/browse_frm/thread/8a2feab451c13f24/5cf24c...


Hey, hey, now, let's not hate by default.

A lot of water under the bridge since then.
--
Uno