Hello,
I'm learning C++ and i want to build a simple program that copy files, remember that i want a program that copy all types of files, not only *.txt or only binary files.

Thanks,
Nathan Paulino Campos

First, open the file for reading in binary mode.
Second, open the output file for writing in binary mode
declare an unsigned char array of size 255

create a while loop and continue while reading

while( infile.read( buf, sizeof(buf) )
{
    size_t len = infile.gcount(); // get the number of bytes read
    outfile.write( buf, len );
}

I was looking for some reference and here is a working code:

ofstream(argv[2]) << ifstream(argv[1]).rdbuf();

Thanks!

If you are using MS-Windows you can call win32 api functions FindFirstFile() and FindNextFile() to get a list of all the files. Then for each of the filenames it returns just select the ones you want to copy. See this example.

Thanks for the help!
+1 Of Reputation, because you help me outside of the question, for my future project.

Thanks!

This article has been dead for over six months. Start a new discussion instead.