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; Why not just be platform-agnostic about it? Fixed (mostly): Code: bool copyfile(const std::string & source, const std::string & dest) { ...

  1. #31
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,167
    Why not just be platform-agnostic about it?
    Fixed (mostly):

    Code:
    bool copyfile(const std::string & source, const std::string & dest)
    {
       std::ifstream in(source.c_str(), std::ios::binary | std::ios::in);
       if(!in)
       {
          return(false);
       }
       std::ofstream out(dest.c_str(), std::ios::binary | std::ios::out);
       if(!out)
       {
          return(false);
       }
       in.exceptions(std::ios::badbit);
       out.exceptions(std::ios::badbit);
       in >> std::noskipws;
       std::copy(std::istream_iterator<unsigned char>(in), std::istream_iterator<unsigned char>(), std::ostream_iterator<unsigned char>(out));
       return(true);
    }
    Soma

  2. #32
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by phantomotap View Post
    Fixed (mostly):
    Good catch on the stream constructors and exception masks.

    As far as the char --> unsigned char change, I don't think it matters.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #33
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,167
    As far as the char --> unsigned char change, I don't think it matters.
    *shrug*

    I don't like the idea of using a type that may be distinct from its signed and unsigned variants. It doesn't make any sense.

    Soma

  4. #34
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You've got to admire the inefficiency of this approach.
    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

  5. #35
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    And that is why I always argue CLI tools should go away, and an API provided instead.
    Oh, but there are APIs these tasks...
    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.

  6. #36
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And that is why I always argue CLI tools should go away
    Because someone might use system() to call them?

    Hey, maybe we should do away with computers. Someone might use them to hack into another.
    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. #37
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by CornedBee View Post
    You've got to admire the inefficiency of this approach.
    I'm not sure it would be all that inefficient. Of course it's more efficient to copy larger blocks rather than bytes at a time, but as far as std::copy and the stream iterators are concerned, that should all optimize away into a loop which gets byte from the input and writes it to the output. Combine that with the buffering and I think it's not too bad. Not that I've benchmarked it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #38
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Stream iterators go via the insertion/extraction operators. These have to construct a sentry object, which does all kinds of useless checks. They have to look for and obey stream state (bad, fail and eof bit). They have to look for and obey formatting settings (in particular setw()). They then call get()/put(), which repeats the state check. They then use the stream buffer to store the data. If the buffer is full, they flush/fill it and will have to check the stream state afterwards.
    I doubt any compiler optimizes all the useless parts away, or even inlines the <</>> calls (which is a prerequisite for opimizing anything away).
    In the course of this action, every byte is typically copied at least five times in memory, and that's assuming pretty good register-holding of the byte.

    At least you should use streambuf iterators for this stuff.

    I know, I know, copying is I/O-bound anyway. But I'm sure the OS copy routine has some optimizations there, too. Maybe there's a disk-to-disk copy command that the disk controller supports, and the data never even has to travel over the bus.
    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

  9. #39
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Eh, you're right. I checked both G++ 3.x (only thing I have around at the moment) and VC8 and the assembly code... sucks.

    I still like how it looks. And we could create a raw byte I/O iterator, but I was just wasting time anyway.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #40
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Did you have to pull out the std stuff?

    CopyFile worked fine, and the person never said he was using linux .
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  11. #41
    Registered User
    Join Date
    Nov 2008
    Posts
    36
    How do you put files/folders inside a .exe?

  12. #42
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Helgso View Post
    How do you put files/folders inside a .exe?
    You mean like a self extracting ZIP file?
    "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

  13. #43
    Registered User
    Join Date
    Nov 2008
    Posts
    36
    Yah, when executed, it would copy files included in itself to a desired location.

  14. #44
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Quote Originally Posted by cpjust View Post
    You mean like a self extracting ZIP file?
    I made a miniproject that did that once. One executable in the project, called "Sourcize", was a program that serialized the subject file into a set of C-style arrays (it had to be a set of them since the compiler I was using puts a cap on the number of elements an array can have) as source code, then another executable that used that source code, which is setup to write the arrays back to a file.
    I'd give you the project, but it seems I can't attach zip files here. What I could do is give you the source code for the array structure.
    The file is in the global g_arrays. Arrays.cpp is an abstraction, btw, and NOT legal C++, rather than the 4Mb file it was before I pruned it for you.
    Attached Files Attached Files

  15. #45
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by brewbuck View Post
    Eh, you're right. I checked both G++ 3.x (only thing I have around at the moment) and VC8 and the assembly code... sucks.

    I still like how it looks. And we could create a raw byte I/O iterator, but I was just wasting time anyway.
    Wouldn't this be valid and faster:
    Code:
    out << in.rdbuf();
    I might be wrong, but I think that is bound to copy each byte properly... Any thoughts?

Page 3 of 4 FirstFirst 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