Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS151 / KCS251 Programming for Problem Solving - Using C Language
Lab Exercises
37. Write a c program to copy & count the character content of one file says a.txt to another file b.txt.
/*
File: Prgrm37.c
Author: Aditya Saini
Date: Jan 20, 2021
Description: Program to copy & count the character content of one file says a.txt to another file b.txt.
*/
#include <stdio.h>
int main (void)
{
FILE *f_ptr_1, *f_ptr_2;
char path_1[256], path_2[256];
char ch;
int no_of_char;
//Input first file path
printf ("Input first file path: ");
fflush (stdin);
gets(path_1);
//Open first file
f_ptr_1 = fopen (path_1, "r");
if (f_ptr_1 == NULL)
{
printf ("Error! Unable to open first file.");
return 0;
}
//Input second file path
printf ("Input second file path: ");
fflush (stdin);
gets(path_2);
//Open second file
f_ptr_2 = fopen (path_2, "w");
if (f_ptr_2 == NULL)
{
printf ("Error! Unable to open second file.");
return 0;
}
//Copy & count
no_of_char = 0;
ch = fgetc (f_ptr_1);
while (ch != EOF)
{
fputc (ch, f_ptr_2);
ch = fgetc(f_ptr_1);
no_of_char++;
}
printf ("\n%d no of characters copied from %s to %s", no_of_char, path_1, path_2);
//Close files
fclose (f_ptr_1);
fclose (f_ptr_2);
};
No comments:
Post a Comment
Please do not post spam links.