Thread: Help to copy files in c++

  1. #16
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Quote Originally Posted by Elysia View Post
    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.
    Yes exactly, which is why I didn't want him to accidentally type \\ and not understand why it didn't work when he's using it with cin.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  2. #17
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Actually on windows you can use single or double slashes, and it will still work.

  3. #18
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Raigne View Post
    Actually on windows you can use single or double slashes, and it will still work.
    You mean you can use a forward slash or a double backslash in the Windows API.
    You can't use a single backslash or a double slash.
    Code:
    "C:/file.txt"  // Good
    "C:\\file.txt" // Good
    "C://file.txt" // bad
    "C:\file.txt"  // bad
    "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

  4. #19
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Hah, thanks for making it incredibly clear .
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    36
    Yay! Everything is working now except one thing.

    I added a code that finds out from what path my exe was executed in and the path is stored in a string. But, when I tell it to copy from that path, and that path includes spaces like so: "C:\Program Files\" instead of "C:\ProgramFiles\" it returns: "C:\Program could not been found."

    So how do I tell it to include the spaces in the path when it copys? Anyone?

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Extra quotes around the file name. Of course, this indicates you're still using system().
    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

  7. #22
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    Extra quotes around the file name. Of course, this indicates you're still using system().
    But make sure you escape the quotes inside the string, otherwise you'll have 2 blank strings with some junk in between that the compiler will complain about:
    Code:
    "\"C:/Program Files/blah/blah\""
    "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

  8. #23
    Registered User
    Join Date
    Nov 2008
    Posts
    36
    Ok great thanks!

    Yet one thing xD

    My program lets users input a string, which is then used to copy the files to. But, if the user types f.x: "c:\program files\" instead of "C:\Program Files\" it doesn't work because the user inputted lower case. So, can I make the program stop being case-sensitive when copying?

    Thanks!

  9. #24
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Windows file systems are always case insensitive by default (although you can make it case sensitive). I've never seen anyone who actually enables case sensitivity of a Windows file system, so I doubt that would be a problem.
    "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

  10. #25
    Registered User
    Join Date
    Nov 2008
    Posts
    36
    If I let the program copy, after the copying it returns either: "1 file(s) copied" or "The system cannot find the file specified". So, can I make the program find out either was returned?

  11. #26
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Helgso View Post
    If I let the program copy, after the copying it returns either: "1 file(s) copied" or "The system cannot find the file specified". So, can I make the program find out either was returned?
    What code are you using to copy the files?
    If you're using system(), then no. If you use the proper Windows API call, it should tell you if it succeeded or not.
    "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

  12. #27
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    What code are you using to copy the files?
    If you're using system(), then no. If you use the proper Windows API call, it should tell you if it succeeded or not.
    Why not just be platform-agnostic about it?

    Code:
    #include <string>
    #include <fstream>
    #include <iterator>
    #include <algorithm>
    
    bool copyfile(const std::string &source, const std::string &dest)
    {
        std::ifstream in(source);
        std::ofstream out(dest);
        if(!in || !out) return false;
        in >> std::noskipws;
        std::copy(std::istream_iterator<char>(in), std::istream_iterator<char>(),
            std::ostream_iterator<char>(out));
        return true;
    }
    No error checking in the actual copy op, of course.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cpjust View Post
    What code are you using to copy the files?
    If you're using system(), then no.
    Except of course you can.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    int main(void) {
        printf("%d\n", system("copy temp.c temp.d"));
        printf("%d\n", system("copy temp.garbage temp.d"));
        return 0;
    }
    Code:
    >temp
            1 file(s) copied.
    0
    The system cannot find the file specified.
    1

  14. #29
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by tabstop View Post
    Except of course you can.
    You're right. For some reason I thought it just returns non-zero if the command interpreter isn't found. Although using system() is still a pretty bad way to do it.
    "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

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cpjust View Post
    You're right. For some reason I thought it just returns non-zero if the command interpreter isn't found. Although using system() is still a pretty bad way to do it.
    That's only when you pass NULL as the argument. And it's the other way -- nonzero if it is found.

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, 09:35 AM