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

S

scad

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
 
V

Victor Bazarov

scad said:
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
 
M

mzdude

    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.
 
V

Victor Bazarov

mzdude said:
optional step
2. Look up the function seek()

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
 
J

James Kanze

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top