Help to copy files in c++

This is a discussion on Help to copy files in c++ within the C++ Programming forums, part of the General Programming Boards category; I'm using the compiler CodeBlocks. This is my program: Code: string input; string output; input = "C:\bla.txt\"; //"Bla.txt" is the ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    36

    Help to copy files in c++

    I'm using the compiler CodeBlocks.

    This is my program:

    Code:
    string input;
    string output;
    
    input = "C:\\bla.txt\\";          //"Bla.txt" is the file to copy
    output = "C:\\test\\";            //"Test" is the folder to copy to
    
    system("copy input.c_str() output.c_str()")
    Here is what I don't know, How do I code the line that includes "system()" to let the program know that I want it to copy the file with the information the string has, to a directory another string has? Is it like this? Because when I do this, the program says: "The system cannot find the file specified."

    So what do I do? I tried hard coding that line and it worked, so I know it has something to do with me writing "input.c_str()". What shall I change for this to work?

    Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    > system("copy input.c_str() output.c_str()")
    There's no magic substitution of "do what I mean" going on here.

    You need to build the command line yourself, eg.
    Code:
    string cmd = "copy ";
    cmd += input;
    // and so on
    system( cmd.c_str() );
    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
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The code as you have it now will execute the shell command "copy input.c_str() output.c_str()". Unless you have a file called "input.c_str()", that won't get you very far.

    You need to build a proper command string by concatenation, then pass that to system().

    Better yet, don't use system at all. Use CopyFile from windows.h. (The copy command is windows-specific, so you don't lose portability.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    36
    When I do CopyFile:

    Code:
    CopyFile("chat_english.c_str()", "C:\\test\\");
    My compiler, CodeBlocks, says: "error: too few arguments to function `BOOL CopyFileA(const CHAR*, const CHAR*, BOOL)'"

    So, what to do?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You look the function up in the help and find out why two arguments aren't enough. What else? How much clearer can an error message be than, "too few arguments"?

    And you still think that code gets executed inside string literals.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    Better yet, don't use system at all. Use CopyFile from windows.h. (The copy command is windows-specific, so you don't lose portability.)
    Actually, I would write my own CopyFile() function that abstracts away the underlying OS and gives a standard interface no matter what OS you're on. That way, when you port to another OS, all you need to do is add another #ifdef block of code in your own CopyFile() function:
    Code:
    bool Filesys::CopyFile( const char*  src, const char*  dest, bool  overWrite )
    {
    #ifdef  WIN32
        // Put your Windows specific code here.
    #elif defined( LINUX )
        // Put Linux specific code here.
    ...
    #endif
    }
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, if I wanted portability, I'd just use Boost.Filesystem.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    Quote Originally Posted by cpjust View Post
    Actually, I would write my own CopyFile() function that abstracts away the underlying OS and gives a standard interface no matter what OS you're on. That way, when you port to another OS, all you need to do is add another #ifdef block of code in your own CopyFile() function:
    Too much for a newbie, I would say.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Code:
    CopyFile(InputString.c_str(), string(OutputString+InputFileNameString).c_str(), TRUE); // true if you want it to fail if it already exists.
    This should work better.

    Remember to include <windows.h>
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Too much for a newbie, I would say.
    True, but something to keep in mind for later on down the road.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    36
    Quote Originally Posted by execute View Post
    Code:
    CopyFile(InputString.c_str(), string(OutputString+InputFileNameString).c_str(), TRUE); // true if you want it to fail if it already exists.
    This should work better.

    Remember to include <windows.h>
    So you're saying, it should be writed like this?:

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        string Input = "C:\\bla.txt\\";
        string Output = "C:\\test\\";
        
        CopyFile(Input.c_str(), string(Output+Input).c_str(), TRUE);
        
        //The file "bla.txt" would now be copied to "test"?
    }
    I do not find the part "string(Output+Input).c_str()" make sense, is this right?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So Output+Input would be the string C:\test\C:\bla.txt, which would not make for a good filename.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    You need a different string, for just the output filename.

    Code:
    string Input = "C:\\bla.txt";
    string CopiedFile = "bla.txt";
    string OutputFolder = "C:\\test\\";
    
    CopyFile(Input.c_str(), string(OutputFolder+CopiedFile).c_str(), TRUE);
    And you can use "cin >> OutputFolder" type thing to get user input about the folder. (You don't have to type "\\" in that case, one slash is enough I believe.

    Just don't use this code in something that has internet access, someone can use your code to copy files into your system.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    Quote Originally Posted by execute View Post
    And you can use "cin >> OutputFolder" type thing to get user input about the folder. (You don't have to type "\\" in that case, one slash is enough I believe.
    What a user types is stored inside a buffer. A double "\\" is only required when coding a \ into code since the \ character is treated as an escape character.

    Just don't use this code in something that has internet access, someone can use your code to copy files into your system.
    Say what?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Like dont make a program, that connects using sockets to the internet, and reads internet data or data provided by other people. An example would be, don't use it as an instant messenger for example.

    If you don't understand don't worry about it, I'm sure you won't use it in the way i described anyway.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. fopen vs. _open (for BIG image files)
    By reversaflex in forum C Programming
    Replies: 3
    Last Post: 04-01-2007, 12:52 AM
  4. Copy files
    By ErikDN in forum C Programming
    Replies: 1
    Last Post: 10-09-2004, 07:50 PM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 08:35 AM

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