Thread: seeking binary files

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    seeking binary files

    Hi,
    Can I seek a file using 2 lseek() at the same time, each one seeks for different thing?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Nothing happens in a single process "at the same time". If they're separate processes then it makes no difference because each has its own internal offset pointer. You'll have to be more specific about what you're actually doing.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, you can't. The second lseek() will start where the first left off.
    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
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You could always do something like this:
    Code:
    #include <stdio.h>
    
    void showline(FILE *fp, long *offset)
    {
      char buf[BUFSIZ];
    
      fseek(fp, *offset, SEEK_SET);
    
      fgets(buf, sizeof(buf), fp);
      fputs(buf, stdout);
    
      *offset = ftell(fp);
    }
    
    int main(void)
    {
      FILE *fp;
      long offsets[2];
      int i;
    
      if(!(fp = fopen("flipflop.txt", "r")))
      {
        puts("Unable to open 'flipflop.txt' for reading!");
        return 1;
      }
    
      offsets[0] = 0;
      offsets[1] = 50;
    
      for(i = 0;i < 5;++i)
      {
        showline(fp, &offsets[0]);
        showline(fp, &offsets[1]);
      }
    
      fclose(fp);
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ cat flipflop.txt
    000000000
    111111111
    222222222
    333333333
    444444444
    555555555
    666666666
    777777777
    888888888
    999999999
    itsme@itsme:~/C$
    Code:
    itsme@itsme:~/C$ ./flipflop
    000000000
    555555555
    111111111
    666666666
    222222222
    777777777
    333333333
    888888888
    444444444
    999999999
    itsme@itsme:~/C$
    That's using standard streams, but the concept is the same for low-level I/O.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying Binary Files
    By mikeman118 in forum C++ Programming
    Replies: 9
    Last Post: 08-11-2007, 10:55 PM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. send/recv binary files using sockets in C++
    By dafatdude in forum Networking/Device Communication
    Replies: 14
    Last Post: 07-25-2004, 11:00 AM
  4. MFC: CStrings & binary files question(s)
    By BrianK in forum Windows Programming
    Replies: 0
    Last Post: 06-24-2004, 05:41 PM
  5. Binary files
    By Brian in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 01:13 PM