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.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;
}

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

you can not test for EOF when working with binary files because that may be a valid character in the file. What I would do is read/write with blocks of data, using fread() and fwrite()

unsigned char buf[255];
size_t size;
while( (size = fread(buf, 1, sizeof(buf), fp) ) > 0)
    fwrite(buf, 1, size, fpW);
#include<stdio.h>
#include<conio.h>

#define SF "E:/aaa.jpg"
#define DF "D:/rrr.jpg"

void main()
{
  int *buf;
  FILE *pr,*pw;
  clrscr();

  pr = fopen(SF,"rb");
  pw = fopen(DF,"wb");
  while(!feof(pr)){
    fread(buf,1,1,pr);
    fwrite(buf,1,1,pw);}
  fclose(pr);
  fclose(pw);

}

Did you test that program? What compiler did you use? The reason I asked is because variable buf is an unallocated pointer, so the program will most likely crash.

>> void main()
main() should be declared as either int main() or int main(int argc, char* argv[]) because it always returns int whether you want it to or not.

line 15: That's the wrong way to code the loop because feof() doesn't work that way. A better way to code it is to use the return value of fread() to know when end-of-file has been reached.

Reading and writing the file one byte at a time will take a very long time with large files. Use a larger buffer, say 256 bytes, and write out only the number of bytes read by fread()

ya I've performed this in TC with transfer of 34MB file and it just take the regular time..
the use of buffer is dependent upon user,.. i've just shown the simple code which actually work very well in TC.. try it..

Turbo C is smart about how it handles this - it won't just move one int at a time.

I use ver. 1.01 of TC, and I get an error on main() without a return int, but it will allow void main() with no return.

Can you use an int pointer when you want to transfer char's however?

ya, int can be used because it reads/write files in binary mode.

I'll have to experiment with that - thanks.

>>I use ver. 1.01 of TC, and I get an error on main() without a return int, but it will allow void main() with no return
TC is a very old and non-standard compiler, so anything is possible I suppose. That doesn't mean your program works correctly.

>>ya, int can be used because it reads/write files in binary mode
What happens of the file size is not evenly divisible by sizeof(int) ? Lets assume the file size is 3 bytes and sizeof(int) is two bytes.

Since int* buf is an uninitialized pointer, where do you suppose TC will store the data it reads from the file? Answer: TC will scribble it all over memory into some random memory location which your program may, or may not own.

>>Can you use an int pointer when you want to transfer char's however?
No. Not because of the difference between integers and charcters, but because use of int requires the file size be a multiple of sizeof(int). See above comment.

No doubt, the int pointer should be given an initial address before you use it!

I believe we used unsigned char's in binary mode, to copy files, back in the Day.

TC was made to comply with the AT&T standard, which was quite good for it's day. Doesn't handle the new stuff, but things like // to rem out a line, are fine.

In my case (ver. 1.01), it's the C++ side of the TC compiler that is nearly useless for today's codes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.