Copy and Move functions?

This is a discussion on Copy and Move functions? within the C++ Programming forums, part of the General Programming Boards category; I've been trying to get a basic Copy function working (Move is what I want, but I figure I'd make ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    New York, New York
    Posts
    19

    Copy and Move functions?

    I've been trying to get a basic Copy function working (Move is what I want, but I figure I'd make a copy function first). I built the function using iostream.h, creating the file in the wanted directory, and reading the source file to the destination file. The file I'm trying to copy is a tiny 1 Kilobyte file (Just a basic Firefox plugin file), but when it copies the file from my desktop into the Firefox searchplugin folder (My destination) the file size becomes a whopping 667 Kilobytes.

    I really don't know what I'm doing wrong, but I made this basic plugin for use between the people who I play an online game with (It is a search plugin to look through the game's database). I know it wasn't nessecary but I figured I'd make a pseudo-installer for it, using command line, so that I could at least get some C++ practice in.

    At the current time I'm at school, but when I get home I'll paste my Cpp file's contents and show you what I'm working with.

  2. #2
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    Might as well not make the thread until you have the code to post, because until then this is kind of ... useless.

    If I had to guess, you take in a bit at a time, and re-write it as a byte. If that makes sense. But that's all anyone can do, is guess.

    Maybe a giant whale made it's home in your files. Just as likely without seeing the code.

    Last edited by simpleid; 10-06-2006 at 10:51 AM.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    I built the function using iostream.h,
    Well, that's mistake #1. iostream.h is not standard.

    Sounds like your program is platform-specific already, so why not use some system API?

    Or alternatively, you could use Boost.Filesystem's rename(), and fall back to copy_file() + remove() if that fails. (rename() is not possible across different drives in Windows).
    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
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    You may use iostream instead of its .h version.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    New York, New York
    Posts
    19
    I thought iostream.h and <iostream> in the header were the same thing? Oh well, here is the code;

    Code:
    void CopyFile(char* src, char* dest) {
         FILE* s;
         FILE* d;
         s = fopen(src,"rb");
         d = fopen(dest,"wb");
         char c;    
         while (true)     
         {         
         c = fgetc(s);        
         if (c == NULL)             
           break;         
         fputc(d,c);     
         } 
    }
    Also, CornedBee, how do you use DOS commands in C++?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > char c;
    c should be declared an int.

    > if (c == NULL)
    This should be:
    Code:
         if (c == EOF)

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by Pobega
    I thought iostream.h and <iostream> in the header were the same thing? Oh well, here is the code;

    Code:
    void CopyFile(char* src, char* dest) {
         FILE* s;
         FILE* d;
         s = fopen(src,"rb");
         d = fopen(dest,"wb");
         char c;    
         while (true)     
         {         
         c = fgetc(s);        
         if (c == NULL)             
           break;         
         fputc(d,c);     
         } 
    }
    Also, CornedBee, how do you use DOS commands in C++?

    1. Iostream.h hasn't been part of C++ since 1998.
    2. None of your code you pasted uses iostream or C++ streams at all, you're using C functions.
    3. You never closed your files, so you could potentially have some or all of the destination file missing.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    New York, New York
    Posts
    19
    I thought that's what all of those functions like fgetc were considered, part of iostream?

    And also, I really don't know the difference between C and C++, so sorry if I put this in the wrong section. It's very confusing because I'm new to this and I'm going right into C++, so any problems I come across I put here.

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    No, those are C functions, from cstdio

    std::ifstream, std::ofstream, and similar classes are the C++ way (which is <fstream> not <iostream> actually).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    You need a tutorial. It is in my signature.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Quote Originally Posted by Pobega
    I thought iostream.h and <iostream> in the header were the same thing?
    No, iostream.h is the header that was prevalent before standard C++ came out. During the standardization process a new header file was created, iostream, to reflect the changes for standard C++. iostream.h is still present, although deprecated (not recommended, for it might be removed sometime in the future), to prevent breaking existing C++ code.

    Oh well, here is the code;

    Code:
    void CopyFile(char* src, char* dest) {
         FILE* s;
         FILE* d;
         s = fopen(src,"rb");
         d = fopen(dest,"wb");
         char c;    
         while (true)     
         {         
         c = fgetc(s);        
         if (c == NULL)             
           break;         
         fputc(d,c);     
         } 
    }
    fgetc() returns an int, not a char. It returns EOF on error, not NULL. (EOF can only be stored in an int, not a char.) You should also definitely check the return value of fopen() (which does return NULL on error).
    Code:
    void CopyFile(char* src, char* dest) {
         FILE* s;
         FILE* d;
         s = fopen(src,"rb");
         d = fopen(dest,"wb");
    
         // Make sure s and d aren't NULL
    
         int c;    
         while (true)     
         {
         c = fgetc(s);
         if (c == EOF)
           break;         
         fputc(d,c);     
         } 
    
         fclose(s);
         fclose(d);
    }
    [edit] And as Cat said you need to close the filestreams. [/edit]

    Also, CornedBee, how do you use DOS commands in C++?
    The system() function will execute a DOS command (at least under Windows); but it's not recommended for security reasons, speed, and portability. You shouldn't use it if you can help it.
    Last edited by dwks; 10-06-2006 at 03:30 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Quote Originally Posted by dwks
    iostream.h is still present,
    Depends on where you look. Visual Studio removed it in the 2003 version.

    although deprecated
    By compilers, not the standard. The standard doesn't mention it.
    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

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Odd, I thought it was deprecated. I must have been thinking of the C header files, which have turned into cheader.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Odd, I thought it was deprecated.
    Me too. If CornedBee is saying it's not mentioned in the C++ Standard, I guess it never existed (as far as standard C++ is concerned).

  15. #15
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Yeah, it was used in C++ before the first set of C++ standards came out. Many of the earliest standard compilers maintained it for backwards compatibility with pre-standard code, but nothing requires them to.
    Last edited by Cat; 10-06-2006 at 05:32 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer functions - nebie question
    By bbresc512 in forum C Programming
    Replies: 3
    Last Post: 03-24-2006, 12:36 PM
  2. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  3. i really need help here
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-09-2002, 10:47 PM
  4. Can't figure out compiler message so I can move on
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-26-2002, 07:27 PM
  5. copy constructor
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2001, 05:17 PM

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