views:

90

answers:

3

Hello all, I want to copy a single file, fairly large (+100MB) using CreateFile(), ReadFile(), and WriteFile().

My program successfully copied text file and other small file (in the range of KBs), but when I wanted to copy a 160 single .EXE file, it crashed and the debugger said "Stack overflow"

+5  A: 

Don't read the whole file at once, read it in smaller chunks (up to a few megabytes) instead.

Windows has several file copy functions that are already quite flexible, e.g. CopyFileEx, so consider using one of these functions instead.

Philipp
The thing is I don't want to use CopyFile(), I know it can make the thing a lot easier. Just wanna use CreateFile() and WriteFile().How do you suggest I break it in smaller chunks?
Saif
@Saif Why do you think that you can write it easier? Existing library code is typically well tested and optimized.
Mark B
Yeah I know, I just have this assignment. I need to do it manually. :S
Saif
Remember to use dynamic memory (a.k.a. heap) for large buffers as the local (stack) and global memory have smaller size limits assigned by the compiler.
Thomas Matthews
Yeah I read about this heap thing, I'll try to dig deeper into it and see what's what.Thank you Thomas.
Saif
A: 

The simpler the better: int rc = system("copy foo.bin bar.bin");

tojas
Somebody could argue that in this way you completely loose control over the copy operation.
Gianluca
And it's invoking a shell, adding unnecessary conceptual overhead.
Philipp
@Gianluca, copying a file is copying a file. What kind of control do you need ? Moreover, this is bug free.@Philipp, what is a conceptual overhead ? Do you mean performance ?
tojas
@tojas For instance, you may want to display a progress bar. A very good practice when performing long processing within a GUI application
Gianluca
A: 

Thank you all for the help and hints. A simple block of a do{...} while() made it possible :)

Saif