How To Write a C Program To Copy The Contents Of One File Into Another Using fputc in C Programming Language ?
Solution For C Program To Copy The Contents Of One File Into Another Using fputc:
#include<stdio.h> #include<process.h> void main() { FILE *fp1,*fp2; char a; clrscr(); fp1=fopen("test.txt","r"); if(fp1==NULL) { puts("cannot open this file"); exit(1); } fp2=fopen("test1.txt","w"); if(fp2==NULL) { puts("Not able to open this file"); fclose(fp1); exit(1); } do { a=fgetc(fp1); fputc(a,fp2); }while(a!=EOF); fcloseall(); getch(); }Output :
Content will be written successfully to file
Explanation of Program :
We have to files with us , we are opening one file in read mode and another file in write mode.
fp1=fopen("test.txt","r");
and
fp2=fopen("test1.txt","w");
It is better practice to check whether file is opened successfully or not using NULL check.
if(fp2==NULL) { //File is Not opened Successfully }
If everything goes right then we are reading file character by character and writing on file character by character.
a=fgetc(fp1); //Reading Single Character
End of File is specified by EOF character, thus if we get EOF character then process of writing on the file will be terminated.
do { a=fgetc(fp1); fputc(a,fp2); }while(a!=EOF);
Tags: C Program To Copy The Contents Of One File Into Another Using fputc, write a c program to copy the contents of one file into another file, c program to copy one file to another file using command line arguments, write a program to copy one file to another file in java, copy one file to another in c++, write a c program to copy one file to another with output, write a program to count the number of times a character appears in the file, c program to copy one file to another in linux, c program to copy one file to another using system calls.
Learn More :
C Program
- Using Bash to input stuff into c program
- Difficult C Programming Questions
- Write a c program to find largest among three numbers using binary minus operator three numbers using binary minus operator
- PRINTING ASCII VALUE USING C PROGRAM
- MULTIPLICATION OF TWO MATRICES USING C PROGRAM
- FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX USING
- Write A C Program To Find Out Transport Of A Matrix
- Factorial of 100 in C Program
- Multiplication of large numbers in c
- Division of Large Numbers in C Program
- BINARY SEARCH USING C PROGRAM
- BINARY SEARCH THROUGH RECURSION USING C PROGRAM
- FIND FACTORIAL OF A NUMBER USING RECURSION IN C PROGRAM
- FIND GCD OF A NUMBER USING RECURSION IN C PROGRAM
- FIND SUM OF DIGITS OF A NUMBER USING RECURSION USING C PROGRAM
- FIND POWER OF A NUMBER USING RECURSION USING C PROGRAM
- REVERSE A NUMBER USING RECURSION IN C PROGRAM
- SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE
- Write A C Program For Swapping Of Two Arrays
- SWAPPING OF STRINGS USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION OF DECIMAL TO BINARY USING C PROGRAM
- CONVERSION OF FAHRENHEIT TO CENTIGRADE USING C PROGRAM
- C or C++ Program To Find Bonus Amount