#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define TMAX 100 // Maximum Length of Text in a Text File
#define MAX 12 // Maximum Length of Text File Name
void create_file()
{
FILE *fp;
char fn[MAX];
char ch;
char text[TMAX];
printf("Enter a filename (.txt): ");
scanf("%s",fn);
fflush(stdin);
fp = fopen(fn,"w+");
if(fp == NULL)
printf("\n ERROR in Opening File \n");
else
{
printf("\n\n Enter the text : ");
gets(text);
fflush(stdin);
for(int i=0;i<strlen(text);i++)
fputc(text[i],fp);
printf("\n\n The Text has been written on '%s'",fn);
}
fclose(fp);
}
void display_file()
{
FILE *fp;
char fn[MAX];
char text[TMAX];
char ch;
printf("Enter a filename (.txt): ");
gets(fn);
fflush(stdin);
fp = fopen(fn,"r+");
if(fp == NULL)
printf("\n ERROR in Opening File \n");
else
{
printf("\n\n The Text has been read on '%s'",fn);
printf("\n\n The Text : ");
while(!feof(fp))
{
ch = fgetc(fp);
printf("%c",ch);
}
fflush(stdin);
}
fclose(fp);
}
void compare_files()
{
FILE *fp1,*fp2;
char fn1[MAX];
char fn2[MAX];
char ch1,ch2;
printf("Enter a 1st filename (.txt): ");
gets(fn1);
fflush(stdin);
printf("Enter a 2nd filename (.txt): ");
gets(fn2);
fflush(stdin);
fp1 = fopen(fn1,"r+");
fp2 = fopen(fn2,"r+");
if(fp1 == NULL || fp2 == NULL || strcmp(fn1,fn2) == 0)
printf("\n ERROR in Opening either Files \n");
else
{
int ind = 0;
printf("\n\n The Texts can be read and compared from '%s' and '%s' ",fn1,fn2);
while((!feof(fp1)) || (!feof(fp2)))
{
ch1 = fgetc(fp1);
ch2 = fgetc(fp2);
if(ch1 != ch2)
{
ind = 1;
break;
}
}
if(ind == 1)
printf("\n\n The Text of both files are distinct. ");
else
printf("\n\n The Text of both files are same. ");
}
fclose(fp1);
fclose(fp2);
}
void main()
{
int cho;
do
{
clrscr();
printf("\n... MENU ...\n");
printf("\n1. Create a file ");
printf("\n2. Display a file ");
printf("\n3. Compare the files ");
printf("\n Hit '0' to exit ");
printf("\n\n Enter the option : ");
scanf("%d",&cho);
switch(cho)
{
case 1 : create_file();
break;
case 2 : display_file();
break;
case 3 : compare_files();
break;
default: break;
}
getch();
}while(cho != 0);
}
No comments:
Post a Comment