Total Pageviews

Saturday, May 7, 2011

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.

Sincerely
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";
    cin>>filename1;
    ifstream in(filename1,ios::in);
    strcpy(filename2,"copy_");
    strcat(filename2,filename1);
    ofstream out(filename2,ios::out);
    while(!in.eof())
    {
        in.get(ch);
        out.put(ch);
    }
    return 0;
//end
}
//====================

No comments:

Post a Comment