reading file

Discussion in 'C Programming' started by ericunfuk, Mar 24, 2007.

  1. ericunfuk

    ericunfuk Guest

    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?
     
    ericunfuk, Mar 24, 2007
    #1
    1. Advertising

  2. ericunfuk

    ericunfuk Guest

    Also, a question about fseek().

    Does fseek() clears the EOF if I don't move beyond EOF?
     
    ericunfuk, Mar 24, 2007
    #2
    1. Advertising

  3. In article <>,
    ericunfuk <> wrote:

    >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.
     
    Richard Tobin, Mar 24, 2007
    #3
  4. "ericunfuk" <> writes:
    > 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) <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"
     
    Keith Thompson, Mar 24, 2007
    #4
  5. ericunfuk

    SM Ryan Guest

    Keith Thompson <> wrote:
    # "ericunfuk" <> writes:
    # > 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?
     
    SM Ryan, Mar 24, 2007
    #5
  6. In article <>,
    SM Ryan <> wrote:

    ># 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.
     
    Richard Tobin, Mar 25, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    699
    Kevin Spencer
    Nov 11, 2004
  2. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,444
    Scott Allen
    Jun 4, 2005
  3. Replies:
    0
    Views:
    807
  4. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    393
    Hrvoje Niksic
    May 25, 2007
  5. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    823
    Alf P. Steinbach
    Jul 10, 2007
Loading...

Share This Page