954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Copying Files by name input

Hey, Im trying to get the user to type in a file name, and then the program will copy the file to some where else like C:\ or USB or something.
So far this is what I've come up with:

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <fstream>
#include <string>

using namespace std;


int main()
{
    string file;
    cout << "Enter name the file, TO THE EXACT WORD!!! eg. \"Hi.txt\" :\n"; 
   
    getline(cin, file);
   
    system ("copy \" file.c_str \" \"C:\\Ok.txt"\ "); 

    getch();
    return 0;
}


The problem there is that when i do file.c_str(), the system thinks is the name of the file, and says it cannot be found.
Thanks for any help..:icon_neutral: :icon_confused:

Dannyo329
Junior Poster in Training
79 posts since Apr 2008
Reputation Points: 20
Solved Threads: 8
 

If you want to do it with the system() command, you could do something like:

int main(void)
{
    string file;
    cout << "Enter name the file, TO THE EXACT WORD!!! eg. \"Hi.txt\" :\n"; 
    getline(cin, file);
    string command = "copy " + file + " C:/Ok.txt";
    system (command.c_str()); 

    cin.get();
    return 0;
}


But I wouldn't recommend it. You might want to read this thread .

You can also see that I've replaced getch() with cin.get(). Getch isn't a standard c++ function and requires you to include the conio header (which also isn't standard). It would be a good thing to learn yourself to use cin.get() in the future to pause your program, or you could just run your program from a console and there wouldn't be a need to pause your program.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Thanks, but I'll just stick to the system() command for now, I'm feeling confused with iterators

Dannyo329
Junior Poster in Training
79 posts since Apr 2008
Reputation Points: 20
Solved Threads: 8
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You