How do I (fseek, ftell, stat)?

C

cedarson

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

Eric Sosman

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.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top