reading file

E

ericunfuk

I'm wondering if the following is fesible.

I copy a whole file into memory, then I traverse forwards and
backwards in the part of the memory contains the file, to get the
chunk of the file I need?
 
E

ericunfuk

Also, a question about fseek().

Does fseek() clears the EOF if I don't move beyond EOF?
 
R

Richard Tobin

ericunfuk said:
I'm wondering if the following is fesible.

I copy a whole file into memory, then I traverse forwards and
backwards in the part of the memory contains the file, to get the
chunk of the file I need?

Perfectly reasonable, if the file is known to be small enough.

For a very large file, where you only need to access small parts of
it, this might be unacceptably inefficient. In such a case you may
find that your operating system provides a way to map files into
memory without reading them unnecessarily, but that's not a standard C
feature. Unix systems provide a function mmap() for this purpose.

-- Richard
 
K

Keith Thompson

ericunfuk said:
Also, a question about fseek().

Does fseek() clears the EOF if I don't move beyond EOF?

What is "the EOF", and what does "beyond EOF" mean?

A more correct statement of your question is:

Does fseek() clear the end-of-file indicator if I don't move beyond
the end of the file?

The answer is yes. C99 7.19.9.2p5 says:

After determining the new position, a successful call to the fseek
function undoes any effects of the ungetc function on the stream,
clears the end-of-file indicator for the stream, and then
establishes the new position.

But why do you care? The end-of-file indicator is set by an attempt
to read past the end of the file. 99% of the time, the correct way to
detect this is to look at the value returned by whatever function you
just used to read from the file; fgetc() returns the value EOF,
fread() returns a number of records smaller than the number you
requested, and so on.

Your system should have documentation that explains all
of this. If you're on a Unix-like system, "man fseek"
should have the answer to your question. For other systems,
the way to read the documentation will vary. And if you're
ambitious, the latest draft of the C standard is available at
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>; it's
not easy reading, but the index and table of contents are good,
and it has the definitive description of the language and of all the
standard library functions.
 
S

SM Ryan

# > Also, a question about fseek().
# >
# > Does fseek() clears the EOF if I don't move beyond EOF?
#
# What is "the EOF", and what does "beyond EOF" mean?

"You never had a single date in high school, did you?"
Buffy Anne Summers
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top