Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,938 questions

51,875 answers

573 users

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 299 views
1 answer 195 views
1 answer 147 views
3 answers 256 views
1 answer 144 views
...