Ex:3 Text Editor
#include<stdio.h>
#include<conio.h>
#include<string.h>
int word=0,line=0,choice;
FILE *fp1,*fp2,*fp3;
char ch,f[20],sf[20],df[20];
void main()
{
void count(void);
void copy(void);
void paste(void);
clrscr();
p:
printf("Menu");
printf("\n1.count \n2.copy \n3.paste \n4.exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
count();
break;
case 2:
copy();
break;
case 3:
paste();
break;
default:
break;
}
if(choice<4)
goto p;
getch();
}
void count()
{
printf("\n Count operation\n");
printf("Enter the file name:");
scanf("%s",f);
printf("\n Input file\n");
fp1=fopen(f,"r");
if(fp1==NULL)
{
printf("file does not exists");
exit(0);
}
ch=getc(fp1);
while(ch!=EOF)
{
if(ch==' ')
word++;
if(ch=='\n')
{
line++;
word++;
}
printf("%c",ch);
ch=getc(fp1);
}
printf("number of lines in input file= %d\n",line);
printf("number of words in input file= %d\n",word);
getch();
fclose(fp1);
}
void copy()
{
printf("\n Enter the source file name:");
scanf("%s",sf);
printf("%s\n",sf);
printf("\n Enter the destination file name:");
scanf("%s",df);
printf("%s\n",df);
fp2=fopen(sf,"r");
if(fp2==NULL)
{
printf("file does not exist");
exit();
}
fp3=fopen(df,"w");
ch=getc(fp2);
printf("\n The content are copied\n");
while(ch!=EOF)
{
putc(ch,fp3);
printf("%c",ch);
ch=getc(fp2);
getch();
}
getch();
fclose(fp2);
fclose(fp3);
}
void paste()
{
printf("\n Enter the source file name:");
scanf("%s",sf);
printf("%s\n",sf);
printf("\nEnter the destination file name:");
scanf("%s",df);
printf("%s\n",df);
fp2=fopen(sf,"r");
fp3=fopen(df,"w");
ch=getc(fp2);
printf("\n The content is pasted\n");
while (ch!=EOF)
{
putc(ch,fp1);
printf("%c",ch);
ch=getc(fp2);
}
fclose(fp1);
fp2=fopen(sf,"w");
fclose(fp3);
fp3=fopen(sf,"w");
getch();
fclose(fp2);
fclose(fp3);
}
Output:
Menu
1.count
2.copy
3.paste
4.exit
Enter your choice:1
Count operation
Enter the file name:source.txt
Input file
ram
raj
sam
number of lines in input file= 3
number of words in input file= 3
Menu
1.count
2.copy
3.paste
4.exit
Enter your choice:2
Enter the source file name:source.txt
source.txt
Enter the destination file name:dest.txt
dest.txt
The content are copied
ram
raj
sam
Menu
1.count
2.copy
3.paste
4.exit
Enter your choice:3
Enter the source file name:source.txt
source.txt
Enter the destination file name:dest1.txt
dest1.txt
The content is pasted
ram
raj
sam
Menu
1.count
2.copy
3.paste
4.exit
Enter your choice:4
No comments:
Post a Comment