473,219 Members | 1,613 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,219 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 3511
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.