Click here to Skip to main content
15,915,164 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I want to create a copy of a binary file. The following code shows how I do this:
C
#include "stdafx.h"
 
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
 
int main(int argc, char *argv[])
{
    FILE *f1,*f2;
    f1=fopen("D:\\Users\\Dharmaraj\\RESUME.doc","rb");
    f2=fopen("1.doc","wb");
    char temp;
    while(1)
    {
        temp=fgetc(f1);
        if(temp==EOF)
            break;
        fputc(temp,f2);
    }
    fclose(f1);
    fclose(f2);
    return 0;
}
It does not work for .doc and .exe files.
Posted
Updated 30-Jun-11 0:59am
v3
Comments
Pete O'Hanlon 30-Jun-11 6:36am    
How do you actually do this? Saying you use the ordinary method doesn't actually tell us what your code is doing at this point. Could you please post that?
Joan M 30-Jun-11 6:40am    
You should post more information here... take a look at the link I posted, but if it doesn't fit your needs, then please, improve your question adding more details and pasting some code... Pete is 100% right...

Rather than using fgetc/fputc to read and write a character at a time, I would look at using fread/fwrite to read and write the whole file.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Jul-11 2:58am    
Oh yes, getc will take so much time... - a 5.
--SA
Without showing us code... we probably won't be able to give you a lot of help...

I guess that the problem is that you've not opened the file in a binary way...

anyway, take a look at this link[^]... a simple goolge search has thrown this as the first result for me...

Let's see if this works for you...
 
Share this answer
 
Since you are including windows.h I believe the windows API copyfile function would suffice here.

http://msdn.microsoft.com/en-us/library/aa363851%28v=vs.85%29.aspx[^]
 
Share this answer
 
v2
Comments
Pete O'Hanlon 30-Jun-11 8:29am    
There's an inherent assumption here that this code is intended to run on Windows. The OP has not stated this.
Guyverthree 30-Jun-11 8:50am    
i had made that assumption based on the #include of windows.h, I have updated my solution accordingly. Safar.
Albert Holguin 30-Jun-11 10:16am    
its a pretty good assumption with that header
windows has API's to copy a file.But i think you are trying to do it through c file operations. So i am giving a small example how to do it,

FILE *f1,*f2;
f1=fopen("D:\\Users\\Dharmaraj\\RESUME.doc","rb");
f2 = fopen("D:\\Users\\Dharmaraj\\RESUME_Copy.doc", "wb");

//First get the file size
fseek(f1, 0, SEEK_END);
int size = ftell(f1);
fseek(f1, 0, SEEK_SET);

//Allocate that much - good for small size files.
unsigned char *buf = new unsigned char[size];

//Read the whole data.
fread(buf, sizeof(unsigned char), size, f1);

//write the data now.
fwrite(buf, sizeof(unsigned char), size, f2);

You can use normal EOF checking with these type of files.since they are not normal text files.But the above code will work for any file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900