Program to open a file in binary, skip X bytes and write the rest ofthe file to a new file

Discussion in 'C++' started by scad, May 27, 2009.

  1. scad

    scad Guest

    How would I approach this? I cannot store the entire file in memory
    (they are 30GB+) but I need to skip the first 292 bytes and write the
    rest to a new file.

    Thanks,

    Scott
    scad, May 27, 2009
    #1
    1. Advertising

  2. Re: Program to open a file in binary, skip X bytes and write therest of the file to a new file

    scad wrote:
    > How would I approach this? I cannot store the entire file in memory
    > (they are 30GB+) but I need to skip the first 292 bytes and write the
    > rest to a new file.


    1. Open the file to read (file_1)
    1.a If file_1 in bad state, report and exit.

    2. Counter = number of bytes to skip
    3. While counter > 0 and not the end of file_1
    read file_1

    3.a. If file_1 in bad state, report and exit.

    4. Open the file to write (file_2)
    4.a If file_2 in bad state, report and exit.

    5. While not the end of file_1
    read file_1 into 'buffer'
    if successful, write 'buffer' to file_2
    if file_2 in bad state, report and exit.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 27, 2009
    #2
    1. Advertising

  3. scad

    mzdude Guest

    Re: Program to open a file in binary, skip X bytes and write the restof the file to a new file

    On May 27, 2:03 pm, Victor Bazarov <> wrote:
    > scad wrote:
    > > How would I approach this?  I cannot store the entire file in memory
    > > (they are 30GB+) but I need to skip the first 292 bytes and write the
    > > rest to a new file.

    >
    >     1. Open the file to read (file_1)
    >     1.a   If file_1 in bad state, report and exit.
    >
    >     2. Counter = number of bytes to skip
    >     3. While counter > 0 and not the end of file_1
    >          read file_1

    optional step
    2. Look up the function seek()
    >
    >     3.a.  If file_1 in bad state, report and exit.
    >
    >     4. Open the file to write (file_2)
    >     4.a   If file_2 in bad state, report and exit.
    >
    >     5. While not the end of file_1
    >          read file_1 into 'buffer'
    >          if successful, write 'buffer' to file_2
    >          if file_2 in bad state, report and exit.


    If I know I'm dealing with large files, I might even check available
    disk space before starting the copy. Hate to get 29G into the copy
    and fail due to lack of disk space.
    mzdude, May 27, 2009
    #3
  4. Re: Program to open a file in binary, skip X bytes and write therest of the file to a new file

    mzdude wrote:
    > On May 27, 2:03 pm, Victor Bazarov <> wrote:
    >> scad wrote:
    >>> How would I approach this? I cannot store the entire file in memory
    >>> (they are 30GB+) but I need to skip the first 292 bytes and write the
    >>> rest to a new file.

    >> 1. Open the file to read (file_1)
    >> 1.a If file_1 in bad state, report and exit.
    >>
    >> 2. Counter = number of bytes to skip
    >> 3. While counter > 0 and not the end of file_1
    >> read file_1

    > optional step
    > 2. Look up the function seek()
    >> 3.a. If file_1 in bad state, report and exit.
    >>
    >> 4. Open the file to write (file_2)
    >> 4.a If file_2 in bad state, report and exit.
    >>
    >> 5. While not the end of file_1
    >> read file_1 into 'buffer'
    >> if successful, write 'buffer' to file_2
    >> if file_2 in bad state, report and exit.

    >
    > If I know I'm dealing with large files, I might even check available
    > disk space before starting the copy. Hate to get 29G into the copy
    > and fail due to lack of disk space.


    I don't think C++ has the means to do that.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 27, 2009
    #4
  5. scad

    James Kanze Guest

    Re: Program to open a file in binary, skip X bytes and write the restof the file to a new file

    On May 27, 9:03 pm, Victor Bazarov <> wrote:
    > scad wrote:
    > > How would I approach this? I cannot store the entire file
    > > in memory (they are 30GB+) but I need to skip the first 292
    > > bytes and write the rest to a new file.


    > 1. Open the file to read (file_1)
    > 1.a If file_1 in bad state, report and exit.


    > 2. Counter = number of bytes to skip
    > 3. While counter > 0 and not the end of file_1
    > read file_1


    You could also just use istream::ignore. (Otherwise, of course,
    you'll also want to decrement counter by the number of bytes
    you've read each time:).)

    > 3.a. If file_1 in bad state, report and exit.


    > 4. Open the file to write (file_2)
    > 4.a If file_2 in bad state, report and exit.


    > 5. While not the end of file_1
    > read file_1 into 'buffer'
    > if successful, write 'buffer' to file_2
    > if file_2 in bad state, report and exit.


    If there's an error in writing the file, I generally like to
    delete it before exiting, so that the corrupt version doesn't
    accidentally get used.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 28, 2009
    #5
    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. =?ISO-8859-1?Q?=A0?=
    Replies:
    0
    Views:
    397
    =?ISO-8859-1?Q?=A0?=
    Jan 10, 2004
  2. FrancisC
    Replies:
    7
    Views:
    460
    Howard
    Oct 10, 2003
  3. FrancisC
    Replies:
    16
    Views:
    721
  4. VanguardLH
    Replies:
    13
    Views:
    1,166
    dorayme
    Mar 2, 2011
  5. Iulian Ilea
    Replies:
    1
    Views:
    291
    pcx99
    Dec 21, 2006
Loading...

Share This Page