Copying a file

This is a discussion on Copying a file within the Windows Programming forums, part of the Platform Specific Boards category; Hi every body how are you? I'm trying to write a program in C++ that does the following: scans a ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    2

    Question Copying a file

    Hi every body how are you? I'm trying to write a program in C++ that does the following: scans a folder for files, selects one of these files at random, copies the selected file to a prespecified folder, then renames this file to a another predifined name. So far I've the part that scans the folder selects a random file, I need help in copying this selected file to a another folder. Can any one please help? It will be greatly appreciated.
    So far I have the following code:


    Code:
    #include<windows.h>
    #include<stdlib.h>
    #include<time.h>
    #include<iostream>
    using namespace std;
    int main()
    {
       srand(time(0));
       WIN32_FIND_DATA buf;
       HANDLE find=FindFirstFile("*.txt",&buf);
       if(find==INVALID_HANDLE_VALUE)
       {
           cout<<"No *.txt file"<<endl;
           system("pause");
           return 0;
       }
       char*file_found=buf.cFileName;
       do
       {
            if(rand()%3==0)//1 case out of 3
            {
                file_found=buf.cFileName;
                break;
            }
       }
       while(FindNextFile(find,&buf));
       FindClose(find);
       cout<<"I selected "<<file_found<<endl;
       system("pause");
       return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    2

    Thank you saleem.

    I solved the problem with the following line of code:

    CopyFile("source.file", "destination.file", false);

    ...where true or fals stands for don't/do overwrite.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21