473,323 Members | 1,551 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,323 software developers and data experts.

reading file

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
5 1954
Also, a question about fseek().

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

Mar 24 '07 #2
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.