473,756 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3553
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
4967
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 read the data. The problem is that when the file grows in size, the access time also increases. I initially used fseek() with option SEEK_SET, but later switched to SEEK_CUR in the hope that this will speed up the access, but there is no...
62
6241
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 stream, offset must be zero, or a value returned by ftell (in which case origin must be SEEK_SET)." If offset is a value returned by ftell (which returns the current file position), and origin is SEEK_SET, then fseek() sets the position to the...
10
5979
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, where text files use CF+LF (0x0d, 0x0a) to mark end-of-line. The file in question, however, was in Unix format, with only LF (0x0a) at the end of each line. First, does the above situation already invoke "implementation defined" or "undefined"...
3
2951
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); /*add by me*/ last = ftell(fp); cout<<"last="<<last<<"\t"; /*add by me*/ -------------------------
20
7548
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 frequently(forwards and backwards) and do fread() from that offset. Or better still, could anyone let me know some good ways to achieve what I need to do as above?Can I get hold of the file and being able to read in without using fread()? Using...
3
3606
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 want it to do is read the second line of the file, however, for some reason, it grabs the third line. I get similar results with fgetpos and fsetpos. Is there something simple I'm missing here? My code is below: /
13
4496
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 I read the bytes. Determining the number of bytes available in a file is done in 5 steps: 1. Use tell(aFile) to get the current position.
0
9455
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10031
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9838
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9708
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8709
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2665
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.