By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,915 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,915 IT Pros & Developers. It's quick & easy.

reading file

P: n/a
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?

Mar 24 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Also, a question about fseek().

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

Mar 24 '07 #2

P: n/a
In article <11*********************@o5g2000hsb.googlegroups.c om>,
ericunfuk <xu***********@gmail.comwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 24 '07 #3

P: n/a
"ericunfuk" <xu***********@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 24 '07 #4

P: n/a
Keith Thompson <ks***@mib.orgwrote:
# "ericunfuk" <xu***********@gmail.comwrites:
# 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

--
SM Ryan http://www.rawbw.com/~wyrmwif/
So....that would make Bethany part black?
Mar 24 '07 #5

P: n/a
In article <13*************@corp.supernews.com>,
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
># What is "the EOF", and what does "beyond EOF" mean?
>"You never had a single date in high school, did you?"
Buffy Anne Summers
I bet you have a database of quotes from every Buffy episode.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 25 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.