How to keep track of the position in a binary file.

This is a discussion on How to keep track of the position in a binary file. within the C Programming forums, part of the General Programming Boards category; hi, I want to have two functions in a program acess the same binary file, but I want the second ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    How to keep track of the position in a binary file.

    hi,

    I want to have two functions in a program acess the same binary file, but I want the second function to strat reading it fro mwhere the first one left off. Aside from counting how many bytes each function reads in, is there another way to record the position in the binary file where the first function finishes? and how is it done?

    thanks.
    Everything is relative...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about simply reusing the file pointer you're already working with? Unless you close the streem, seek some place else, rewind it, or something like that, it'll be where you left off.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    If you can't reuse the same stream, use ftell() and fseek().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    So if I have the file pntr as a global variable I can not clode the in stream and use it by another function in the program?

    or I can save the position returned by ftell to a global variable and then fseek to it once the file is opened again. and will it return an int, the byte number the pointer is at?
    Everything is relative...

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I actually tried, saving the return of ftell() to a global int and it seemed to work.


    thanks guys,

    AS.
    Everything is relative...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21