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

Ok, let me know how it goes

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 );
}

Thanks, but how i can do a code for only copying files, like *.txt, *.mp3, *.mpg...

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, i use Linux, but this will be very good to port my program to Windows.
Thanks!

If you are using *nix then use opendir() and readir() functions. You will find examples here.

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

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.