Thursday, 17 July 2014

C program to find the largest of 3 files

/*
   Program to read contents of 3 files and find the largest file.
*/



#include<stdio.h>
#include<conio.h>
void main()
{
 FILE *f1,*f2,*f3;
 char f1n[40],f2n[40],f3n[40],tr;
 long size_f1=0,size_f2=0,size_f3=0;

 clrscr();

 printf("Enter the first file name:");
 scanf("%s",f1n);
 printf("\nEnter the second file name:");
 scanf("%s",f2n);
 printf("\nEnter the third file name:");
 scanf("%s",f3n);

 f1=fopen(f1n,"r");
 f2=fopen(f2n,"r");
 f3=fopen(f3n,"r");
 if(f1 && f2 && f3)
   {
    while(1)
   {
tr=fgetc(f1);
if(tr==EOF)
 break;
size_f1++;
   }
    while(1)
   {
tr=fgetc(f2);
if(tr==EOF)
 break;
size_f2++;
   }
    while(1)
   {
tr=fgetc(f3);
if(tr==EOF)
 break;
size_f3++;
   }
    printf("\nFile-1 size=%ld",size_f1);
    printf("\nFile-2 size=%ld",size_f2);
    printf("\nFile-3 size=%ld",size_f3);

 if((size_f1>size_f2) && (size_f1>size_f3))
   printf("\n\nFile-1 is largest");
   else if((size_f2>size_f1) && (size_f2>size_f3))
  printf("\n\nFile-2 is largest");
   else if((size_f3>size_f1) && (size_f3>size_f2))
printf("\n\nFile-3 is largest");
 }
 else
   printf("Error! Invalid file name");

 fclose(f1);
 fclose(f2);
 fclose(f3);

 getch();
}

No comments:

Post a Comment