Copying Files by name input

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Copying Files by name input

 
0
  #1
May 12th, 2009
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:
  1.  
  2. #include <iostream>
  3. #include <windows.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6. #include <fstream>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. int main()
  13. {
  14. string file;
  15. cout << "Enter name the file, TO THE EXACT WORD!!! eg. \"Hi.txt\" :\n";
  16.  
  17. getline(cin, file);
  18.  
  19. system ("copy \" file.c_str \" \"C:\\Ok.txt"\ ");
  20.  
  21. getch();
  22. return 0;
  23. }
  24.  

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..
C programmers will get smashed by C++ programmers.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,971
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Copying Files by name input

 
0
  #2
May 12th, 2009
If you want to do it with the system() command, you could do something like:
  1. int main(void)
  2. {
  3. string file;
  4. cout << "Enter name the file, TO THE EXACT WORD!!! eg. \"Hi.txt\" :\n";
  5. getline(cin, file);
  6. string command = "copy " + file + " C:/Ok.txt";
  7. system (command.c_str());
  8.  
  9. cin.get();
  10. return 0;
  11. }

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Re: Copying Files by name input

 
0
  #3
May 13th, 2009
Thanks, but I'll just stick to the system() command for now, I'm feeling confused with iterators
C programmers will get smashed by C++ programmers.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC