How to copy contents from one file to another in C

1 Answer

0 votes
#include <stdio.h>
  
void readFile(char file[]) {
    FILE *fp = fopen(file, "r");
       
	char ch;
    while ((ch = fgetc(fp)) != EOF)
        putchar(ch);

    fclose(fp);
}
   
void copyFile(char file[], char tofile[]) {
    FILE *fp = fopen(file, "r");
    FILE *tofp = fopen(tofile, "w");
      
    char ch;
    while ((ch = fgetc(fp)) != EOF)
        fputc(ch, tofp);
       
    fclose(fp);
    fclose(tofp);
}
   
int main()
{
    char file[100] = "d:\\data.txt";
    char tofile[100] = "d:\\data_copy.txt";
   
    copyFile(file, tofile);
    readFile(tofile);
   
    return 0;
}
    
      
      
      
/*
run:
      
c c++ c#
java python 
   
*/

 



answered Jul 8, 2020 by avibootz
edited Jul 8, 2020 by avibootz

Related questions

3 answers 272 views
1 answer 179 views
1 answer 124 views
3 answers 230 views
1 answer 125 views
...