473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How do I (fseek, ftell, stat)?

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
2 3528
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
Thanks, Eric. You explain it thoroughly.

Feb 14 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Leslaw Bieniasz | last post by:
Hello, I am trying to fastly read large binary files (order of 100-200 MB) using ftell() and fseek(). My class gets a pointer to the data stored in the file, and then uses fseek() to access and...
62
by: Christopher Benson-Manica | last post by:
On thinking about the "replace a word in a file" thread, I wondered how easy it would be to accomplish the same thing with only one file pointer. This led me to some questions... "For a text...
10
by: Kenneth Brody | last post by:
I recently ran into an "issue" related to text files and ftell/fseek, and I'd like to know if it's a bug, or simply an annoying, but still conforming, implementation. The platform is Windows,...
3
by: Chen ShuSheng | last post by:
HI, I am now study a segment of codes: ------------------------ printf("%p\t",fp); /*add by me*/ fseek(fp, 0L, SEEK_END); /* go to end of file */ printf("%p\t",fp); ...
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
3
by: dfelikson | last post by:
I have a simple piece of code that ftell's me the position after reading the first line of a file, then reads two more lines, then fseek's back to the position, and reads the next line. What I...
13
by: thomas.mertes | last post by:
Hello Recently I discovered some problem. I have some C code which determines how many bytes are available in a file. Later I use this information to malloc a buffer of the correct size before...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.