Copying a file from a variable?

This is a discussion on Copying a file from a variable? within the C++ Programming forums, part of the General Programming Boards category; Hey everyone.. its been so long since i've posted here.. So my question is: Is there a way to copy ...

  1. #1
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605

    Copying a file from a variable?

    Hey everyone.. its been so long since i've posted here..

    So my question is: Is there a way to copy a file from one location to another using variables?

    I'd really appreciate the help- Oh, and i'm using Dev-C++ 4 as a compiler.

    Thanks in advance!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Probably. What are the variables supposed to do?

  3. #3
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    I may have explained that wrong.

    Say i've got one variable containing the path of a file to be copied and another variable with the path of where it's supposed to be copied to.

    Now all I need is to figure out how to copy it.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, you could sprintf those paths into a string, and use it in a system() call.

  5. #5
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    Okay, I tried that.. but im totally confused on the sprintf() function. It's been so damn long since i've done anything in C++.

    Here's what I tried:

    Code:
    char pathtocopyfrom[256];
    char pathtocopyto[256];
    char doit[256];
    sprintf(doit, "copy ", pathtocopyfrom, pathtocopyto);
    printf(doit);
    All that sprintf is storing to my variable is the string "copy". Im totally lost.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need %s to denote where your string variable should go.

  7. #7
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    Can you hook me up with an example?

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    const char source[] = "C:/SomeFile.txt";
    const char dest[] = "D:/NewFile.txt";
    char buffer[256];
    
    sprintf( buffer, "copy %s %s", source, dest );
    system( buffer );

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    sprintf(doit, "copy %s %s", pathtocopyfrom, pathtocopyto);
    FYI: A google on "C++ sprintf" only returned 1,060,000 hits. And technically, this is really C; I imagine the C++ people would actually use a stringstream or similar.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Quote Originally Posted by cpjust View Post
    Code:
    const char source[] = "C:/SomeFile.txt";
    const char dest[] = "D:/NewFile.txt";
    char buffer[256];
    
    sprintf( buffer, "copy %s %s", source, dest );
    system( buffer );
    Are we working on Linux now all of a sudden or some Windows 8?
    Last time I checked, Windows used \ and not /!
    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.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by tabstop View Post
    Code:
    sprintf(doit, "copy %s %s", pathtocopyfrom, pathtocopyto);
    FYI: A google on "C++ sprintf" only returned 1,060,000 hits. And technically, this is really C; I imagine the C++ people would actually use a stringstream or similar.
    That's not C, that's shell script.

    The C++ way to do this is this:
    Code:
    outputFile << inputFile.rdbuf();
    Where input file and output file are c++ file streams, that have been constructed or opened with the proper file names.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Are we working on Linux now all of a sudden or some Windows 8?
    Last time I checked, Windows used \ and not /!
    In C/C++ you can use "/" or "\\" for Windows path separators. Try it and see.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    You are lucky that CreateFile actually supports / instead of \, because that's why it works. However, this doesn't:

    C:\>copy C:/test.txt C:/test2.txt
    Bad arguments.

    Oops. Guess we need to use \ after all!
    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.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Use the CopyFile API, or better yet, use boost::filesystem::copy.
    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

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    You are lucky that CreateFile actually supports / instead of \, because that's why it works. However, this doesn't:

    C:\>copy C:/test.txt C:/test2.txt
    Bad arguments.

    Oops. Guess we need to use \ after all!
    Any system() calls would obviously be limited to what the OS shell supports.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. copying binary file
    By samc2004 in forum C Programming
    Replies: 5
    Last Post: 12-09-2003, 01:34 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM

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