You are on page 1of 17

DESIGN PROBLEM On TEXT EDITOR Submitted by:NAME:-AJAY KUMAR ROLL NO:-A38 SEC NO:-K2R13 SUB:-CSE207T Submitted to:Mr.

Sandeep Ranjan

CODE
#include<stdio.h>//Header file for file operation #include<stdlib.h>//for exit function int i,j,ec,fg,ec2,countc=0,countw=0,row=1,col; char fn[20],e,c; FILE *fp1,*fp2,*fp; void show();//Function for show menu void Create();//for create a new file void Append();//for edit in file from last void Delete();//delete a file which exist void Display();//for show content of a file void menu();//contains many options as new file, open and etc void passwd();//for controlling the users void search();//for search a string into file void count();//for counting of words ,character and many more void main() { system("clear");//clear the screen passwd();//call of function passwd() //global variables

void search() { char str[20]; printf("\n\n\t\tEnter string to be searched:"); gets(str); fp1=fopen("temp.txt","w+");//open file temp.txt system("grep '$str' temp.txt");//searching } void show() { int key; printf("press following key:\n"); printf("\t___________________________________________________\n"); printf("\t|1.Menu|\t|2.Search|\t|3.View|\t|4.Help|\n"); working //option for

printf("\t____________________________________________________\n"); scanf("%d",&key); switch(key) { case 1:menu();

break; case 2:search(); break; case 3:exit(0); } }

void count() { printf("\nTotal:\n\n\t"); printf("Characters:%d",countc);//taken from function create() printf("\n\twords:%d",countw); printf("\n\tRows:%d",row); col=countc+countw; if(col>=70) { col=0;//set 0 } else { printf("\n\tColums:%d",col); }

} void passwd() { char name[28]; int age; int pass; int pas; int ent; pass1: printf("\n\n\n\nEnter your name:"); gets(name);//entry of user name for controlling of using printf("\nEnter you age:"); scanf("%d",&age); //entry of user age for controlling of using pass=rand()/100000+age+((int)name)/10000;//generating a password printf("\n\n\nYou password is:%d",pass); printf("\nPlease remember you password"); pass2://label for furthet uses printf("\n\n\n\n\t\tEnter your password:"); scanf("%d",&pas); system("clear");//for clear the screen if(pas==pass)

{ show(); //menu(); } else { system("clear"); printf("\n\n\n\t\tWrong password\n"); printf("\nPress desire key:\n"); printf("\n1.Re-generate your password:"); printf("\n2.Re-enter you password"); printf("\n0.Exit\n"); scanf("%d",&ent); switch(ent) { case 1: goto pass1; break; case 2: goto pass2; break; case 0:

exit(0); } } } void menu() { do { //printf("\n************************************TEXT EDITOR*******************************"); printf("\n1.NEW FILE\n2.OPEN\n3.OPEN WITH EDITING\n4.DELETE\n0.EXIT"); printf("\n\tEnter your choice: ");//menu for operation scanf("%d",&ec); system("clear"); printf("\n\n\n\n\n\tPress V :"); switch(ec) { case 1: Create(); break; case 2: Display(); /*operations via switch case*/

break; case 3: Append(); break; case 4: Delete(); break; case 5:search(); break; break; case 0: exit(0); } }while(1);

} void Create() { system("clear"); show(); int countw1=0,rows=1,countc1=0; fp1=fopen("temp.txt","w+");//already existing a file

printf("\n\tEnter the text and press Esc to save\n\n\t"); while(1) { c=getchar();//input character by character countc++;//count number of characters fputc(c,fp1);//write in file pointer fp1 if(c==13)//for purpose of \n counting { rows++;//count number of rows row=rows; } else if(c==32)//for spce is entered { countw1++;//count number of words countw=countw1; } else if(c==8)//for checking backspace { countc1--;//decrease character by 1 if backspace is entered countc=countc1; } else if(c == 27)//for cheking entry of ESCAPE

{ fclose(fp1); count(); ff: printf("\n\tEnter then new filename: ");//for saving a file scanf("%s",fn); if(fn=="."||fn=="?"||fn=="/"||fn=="!"||fn=="@"||fn=="#"|| fn=="$"||fn=="%"||fn=="^"||fn=="&"||fn=="*"||fn=="(") { printf("\n\n\n\n\n\t\t\tBad File format try again\n"); goto ff; } else { fp1=fopen("temp.txt","r"); fp2=fopen(fn,"w"); while(!feof(fp1)) { c=getc(fp1); putc(c,fp2); //copy one file to another file } fclose(fp2); /*saving of file*/

system("clear"); menu(); }} }} void Display() { show(); printf("\n\tEnter the file name: ");//entry of file for display scanf("%s",fn); fp1=fopen(fn,"r"); if(fp1==NULL)//check if file does not exist { printf("\n\tFile not found!"); goto end1; } while(!feof(fp1)) { c=getc(fp1); printf("%c",c); } end1: fclose(fp1);

menu(); } void Delete() { show(); printf("\n\tEnter the file name: "); scanf("%s",fn); fp1=fopen(fn,"r"); if(fp1==NULL)//check existance of file { printf("\n\tFile not found!"); goto end2; } fclose(fp1); if(remove(fn)==0)//deleting { printf("\n\n\tFile has been deleted successfully!"); goto end2; } else printf("\n\tError!\n"); end2: printf("\n\n\tPress any key to continue");

menu(); }

void Append() { show(); printf("\n\tEnter the file name: "); scanf("%s",fn); fp1=fopen(fn,"r"); if(fp1==NULL) { printf("\n\tFile not found!"); goto end3; } while(!feof(fp1)) { c=getc(fp1); printf("%c",c); } fclose(fp1); printf("\n\tType the text and press ESCAPE to append.\n"); fp1=fopen(fn,"a");

while(1) { c=getchar(); //if(c==19) //goto end3; if(c==13) { c='\n'; printf("\n\t"); fputc(c,fp1); } else if(c==27)//check the Escape for saving { menu(); } else { printf("%c",c); fputc(c,fp1); } } end3: fclose(fp1);

system("clear"); menu(); }

Input & Output


Enter your name:ajay kumar Enter you age:20

Press V :press following key: ___________________________________________________ |1.Menu| |2.Search| |3.View| |4.Help|

____________________________________________________

Enter the text and press Esc to save ajay is a good boy

Total: Characters:33 words:6 Rows:1 Colums:39 Enter then new filename: raj

You might also like