943,631 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 557
  • C++ RSS
May 12th, 2009
0

Copying Files by name input

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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..
Similar Threads
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008
May 12th, 2009
0

Re: Copying Files by name input

If you want to do it with the system() command, you could do something like:
C++ Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
May 13th, 2009
0

Re: Copying Files by name input

Thanks, but I'll just stick to the system() command for now, I'm feeling confused with iterators
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: composition,aggregation,simple association
Next Thread in C++ Forum Timeline: Coding error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC