Thread: Console Copy Program in C++

  1. #1
    Registered User kaveh8's Avatar
    Join Date
    May 2011
    Iran , Gorgan

    Console Copy Program in C++

    I would like to write a console copy program in C++ that copies a file into another file like linux or windows copy programs.this programs take name of file and copy into the same folder with the same name concatenate it to a "copy_" word.this is a simple program but i would like to develop it's features.
    there are some problems at this way.when you give a executable program name to this simple copy program , the new "copy_executablefile" can't run.for other files such as files with 'pdf' extension it has no problems and copied file (copy_pdfextensionfile) is exactly like the same original file.but in executable file i have a problem.after some check and compare between original file and copied file byte by byte using "Hex Editor" program i discovered that at the end of copied file one byte with 00H value is extra.after removing this extra byte using Hex Editor ,copied file runs successfully.but i can't find out why this extra byte appends into copy_file.what do you know about this prob?please help me.perhaps copying character by character is the reason.because a character is 4byte.what's the solution?
    you can see this simple prog following.

    Kaveh Shahhosseini 6/May/2011

    //this prog tested in Ubuntu 10.10 with Gcc compiler and works correctly.
    //begining of copy program.
    #include <iostream>
    #include <fstream>
    #include <string.h>
    using namespace::std;
    int main()
        char filename1[12],ch,filename2[18];
        cout<<"Enter file name:\n";
        ifstream in(filename1,ios::in);
        ofstream out(filename2,ios::out);
        return 0;

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Open the file in BINARY mode.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    Also, for the sake of sanity, change your char arrays to std::string and cin >> filename1 to std::getline(filename1, std::cin).
    And finally, get rid of strcpy and strcat and use standard operator (=, +=).
    And don't use eof as loop control. That's why you get the extra character.
    I would suggest:
    char c;
    while (stream.get(c))
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 09:42 AM
  2. Copy and Paste function in console apps.
    By Hakins90 in forum C Programming
    Replies: 5
    Last Post: 12-27-2007, 05:07 AM
  3. C program to copy log file frm database
    By nirmala.s in forum C Programming
    Replies: 15
    Last Post: 11-27-2006, 11:05 AM
  4. Making a program copy itself
    By kzar in forum C Programming
    Replies: 6
    Last Post: 05-29-2006, 03:13 AM
  5. Copy To Cliipboard (console app)
    By GaPe in forum Windows Programming
    Replies: 2
    Last Post: 12-10-2003, 05:53 PM