954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Copy file (txt, image, pdf or any other file)

I want to write a Code in C which can create copy any given file (txt, bmp, jpg, pdf etc) what i've written so far is:


int main(){

FILE *fp;
char ch;
FILE *fpW;

if((fpW = fopen("file2.bmp","w"))==NULL) {
printf("Cannot open Destination file.\n");
exit(1);
}

if((fp = fopen("file.bmp","r"))==NULL) {
printf("Cannot open Source file.\n");
exit(1);
}

while((ch = fgetc( fp )) != EOF) {
fputc(ch, fpW);
}

fclose(fp);
fclose(fpW);
//printf("\n %s \n",ret);

return 0;
}

it works fine with txt file but doesn't work with pdf and image files .... please Help ..!!

adi.shoukat
Newbie Poster
23 posts since Sep 2009
Reputation Points: -5
Solved Threads: 0
 

Open files in binary mode.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
Open files in binary mode.


you mean i only need to replace if((fp = fopen("file.bmp","r"))==NULL) with if((fp = fopen("file.bmp","rb"))==NULL)
and remaining code will remain the same as i've posted above ????

adi.shoukat
Newbie Poster
23 posts since Sep 2009
Reputation Points: -5
Solved Threads: 0
 
Open files in binary mode.


Now i've changed my code to:

int main(){


FILE *fp;
char ch;
FILE *fpW;


if((fpW = fopen("file2.gif","wb"))==NULL) {
printf("Cannot open Destination file.\n");
exit(1);
}

if((fp = fopen("file.gif","rb"))==NULL) {
printf("Cannot open Source file.\n");
exit(1);
}

while((ch = fgetc( fp )) != EOF) {

fputc(ch, fpW);
}

fclose(fp);
fclose(fpW);
//printf("\n %s \n",ret);

return 0;
}


but it still isn't working ... plz help :(

adi.shoukat
Newbie Poster
23 posts since Sep 2009
Reputation Points: -5
Solved Threads: 0
 

ch should be an int.

[edit]Using code tags helps get/keep my attention.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

ch should be an int.

[edit]Using code tags helps get/keep my attention.


But i want to print char by char ... wat's the logic to take it int ???

Plz explain ... and tell me, if possible, how should i change my code???

adi.shoukat
Newbie Poster
23 posts since Sep 2009
Reputation Points: -5
Solved Threads: 0
 
But i want to print char by char ... wat's the logic to take it int ???

Correctness? http://web.archive.org/web/20050207005628/http://dev.unicals.com/papers/c89-draft.html#4.9.7.1

int fgetc(FILE *stream);


http://web.archive.org/web/20050207005628/http://dev.unicals.com/papers/c89-draft.html#4.9.7.3

int fputc(int c, FILE *stream);
Plz explain ... and tell me, if possible, how should i change my code???
ch should be an int.
Open files in binary mode.
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: