Reading in a binary file from offset into another file

This is a discussion on Reading in a binary file from offset into another file within the C++ Programming forums, part of the General Programming Boards category; Hi all, I want to read the contents of a binary file from say offset 1E9AC to offset 2E9AC and ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    Reading in a binary file from offset into another file

    Hi all,

    I want to read the contents of a binary file from say offset 1E9AC to offset 2E9AC and everything read in gets saved as another binary file.

    So it is basically creating a new binary file from part of another one. Im struggling a bit with how to do it though. Any ideas?

    Thanks

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    1) read byte by byte until you've read 1E9AB characters. Continue reading and saving until you reach the 2E9ACth character. Output the buffer.

    1a) read byte by byte until you've read 1E9AB characters. Continue reading and writing until you reach the 2E9ACth character.

    2) read 10000 characters into a buffer. Read the next E9AB characters into the buffer. Then read in the next 10000 bytes. Output the buffer.

    3) use one of the "seek()" file functions that places the file pointer at the requested byte. Then read in the next 10000 bytes. Output the buffer.
    Last edited by WaltP; 05-23-2006 at 01:00 PM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    May 2006
    Location
    Berkshire, UK
    Posts
    29
    You could use the C++ method:

    Code:
    #include <fstream>
    using namespace std;
    ...
    ifstream in("InFilename", ios::in | ios::binary);
    ifstream out("OutFilename", ios::out | ios::binary);
    char ch;
    if(!in)
    { //Failed to open file
     ...
     return 1;
    }
    in.seekg(NUMBER_OF_BYTES_TO_SKIP, ios::beg);
    for(int i=0;i<NUMBER_OF_BYTES_TO_READ;i++)
    { if(in.get(ch))
        out << ch;
      else
        break;
    }
    Sorry if it is a bit thrown together. Hope it helps.

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>ifstream in("InFilename", ios::in | ios::binary);
    >>ifstream out("OutFilename", ios::out | ios::binary);

    wouldn't

    ifstream in (""InFilename", ios::binary);
    ofstream out(""OutFilename", ios::binary);

    make more sense? Is there any particular reason to open
    ifstream in ios::out - is that even legal?
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Registered User
    Join Date
    May 2006
    Location
    Berkshire, UK
    Posts
    29
    Quote Originally Posted by Richie T
    Is there any particular reason to open
    ifstream in ios::out - is that even legal?
    Actually I meant to use fstream - that would be it. Late at night :o

    ios::in specifies that the file is capable of input
    ios::out specifies that the file is capable of output
    Last edited by michaels-r; 05-23-2006 at 04:52 PM.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    thanks for the help all
    Last edited by cloudy; 05-24-2006 at 03:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Problem reading a delimited file into a binary tree
    By neolyn in forum C++ Programming
    Replies: 10
    Last Post: 12-09-2004, 06:51 PM
  4. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 01:00 PM
  5. reading from structs in a binary file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 12-21-2001, 09:52 AM

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