I am trying to make a c programe which will work like dos copy command.
I am able to copy the one txt file but this programe is not working with other extension file.

Void main()
{
FILE *fp,*fp1;
char c;

fp=fopen("source","r");
fp1=fopen(" destination","w");

while((c=getc(fp))!=EOF)
putc(c,fp1);
fclose(fp1);
fclose(fp);
}

A text file will be given certain translations (like a newline char gets changed into a CR/LF combination of two chars).

Open the files in binary mode: "rb" and "wb", for sure. Then you *may* need to change char c to an unsigned char.

And on the forum, highlight your code, and click on the [code] tag icon at the top of the editing window. That makes your code stay formatted properly, otherwise it all gets squished over to the far left hand margin and has the wrong font, also.

A text file will be given certain translations (like a newline char gets changed into a CR/LF combination of two chars).

Open the files in binary mode: "rb" and "wb", for sure. Then you *may* need to change char c to an unsigned char.

And on the forum, highlight your code, and click on the [code] tag icon at the top of the editing window. That makes your code stay formatted properly, otherwise it all gets squished over to the far left hand margin and has the wrong font, also.

i did as you said but it's not working.
I am trying to create the copy of jpeg and exe file but it is not workin.

Here is one way to do it

int main()
{
FILE* fp;
unsigned char* buf = 0;
unsigned int size = 0;
// open the jpg file in binary mode
fp = fopen("filename.jpg","rb"); 
if(fp == NULL)
{
   printf("Error\n");
   return 1;
}
// get file size
fseek(fp,0, SEEK_END);
size = ftell(fp1);
buf = malloc(size);
// back to start of file
fseek(fp,0, SEEK_BEG);
// read the file
fread(buf,1,size,fp);
// close the file
fclose(fp);

// now write out to new file
fp = fopen("newfile.jpg","wb");
fwrite(buf,1,size,fp);
fclose(fp);
free(buf);
}

Sorry Gameon, but I couldn't get char to work. /face palm

This is your program, in working trim:

#include <stdio.h>

int main() {
  int i;
  FILE *fin,*fout;
  int c;
 
  fin =fopen("test1.jpg","rb");
  fout=fopen("test2.jpg","wb");

  if(fin==NULL) {
    printf("Error opening input file\n");
    return 1;
  }
  if(fout==NULL) {
    printf("Error opening output file\n");
    return 1;
  }
  while((c=fgetc(fin))!=EOF) 
    fputc(c, fout);

  fclose(fin);
  fclose(fout);

  printf("\n\n\t\t\t    press enter when ready");
  i=getchar(); ++i;
  return 0;
}
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.