By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,679 Members | 1,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,679 IT Pros & Developers. It's quick & easy.

How do I (fseek, ftell, stat)?

P: n/a
I am writing a program and have been instructeed to use the 'fseek',
'ftell', and 'stat' functions, however, after looking in the online
manual for each of these, I am still unsure on how to use them. In my
program, I am to write a code that opens a file, uses 'stat' to
determine the file size, use 'fseek' to move the offset of the pointer,
and finally use 'ftell' to obtain the file pointer index. Will someone
please help? Again, thanks for your help on my other post all who
replied. You help is greatly appreciated.

Feb 14 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ce******@gmail.com wrote:
I am writing a program and have been instructeed to use the 'fseek',
'ftell', and 'stat' functions, however, after looking in the online
manual for each of these, I am still unsure on how to use them. In my
program, I am to write a code that opens a file, uses 'stat' to
determine the file size, use 'fseek' to move the offset of the pointer,
and finally use 'ftell' to obtain the file pointer index. Will someone
please help? Again, thanks for your help on my other post all who
replied. You help is greatly appreciated.


Some files can only be processed from the beginning
to the end: when you are reading from a keyboard or writing
to a socket, the only datum you can read or write is the
"next" byte. This is "sequential access."

Some files may permit you to jump around: read some
data near the beginning, then leap to a spot near the end
and read some more, then reposition to someplace in the
middle and overwrite what was there before. This is
"random access." fseek() is the repositioner: without it
the next read or write operation begins at the byte after
the last one read or written, but fseek() changes the
"current position" to some other arbitrary spot in the file
so you can read or write starting at that spot instead.

ftell() tells you where you are: If you've read or
written some amount of data from some starting position,
ftell() reports the position at which the next read or write
would begin, if you were to perform one. It's useful when
you want to "bookmark" a spot in the file, when you want to
remember it as a place you might want to return to.

There's another pair of functions that can do a similar
thing: fgetpos() reports the current position within a file,
and fsetpos() returns to a reported position. Conceptually
they are like ftell() and fseek(), but the interface allows
for a more flexible implementation. For example, ftell() and
fseek() use a `long' value to describe a position within a
file; on systems where files can be larger than LONG_MAX
characters, they are impotent but fgetpos() and fsetpos()
will work anyhow.

Finally, stat(): It is not a C function, but is specified
by POSIX and perhaps other related standards. Supposedly it
reports the "size" of a file -- but a file's "size" can be a
slippery notion. For example, on Windows a file containing
the two lines "Hello,\nworld!\n" most likely is sixteen, not
fourteen, bytes long: Windows ordinarily inserts an invisible
'\r' character before each '\n' written to a file. On POSIX
systems such things don't happen, but C runs on systems that
are outside the bounds of POSIX, so you cannot count on stat()
to deliver an accurate notion of a file's "size." (If you're
not on a POSIX system, you can't even count on stat() being
present!)

You've got a good deal more reading to do; I hope the
above provides a framework that helps you understand what
you must read.

--
Eric Sosman
es*****@acm-dot-org.invalid
Feb 14 '06 #2

P: n/a
Thanks, Eric. You explain it thoroughly.

Feb 14 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.