AIM:
To write a C program to implement simple text editor.
ALGORITHM:
STEP 1: Start the program execution.
STEP 2: List the menu.
STEP 3: Get the choice.
STEP 4: If choice is 1, create a new file.
STEP 5: If choice is 2, view the required file.
STEP 6: If choice is 3, get the file name to be edited, enter the new text.
STEP 7: If choice is 4, delete the file.
STEP 8: If choice is 5, exit.
STEP 9: Stop the program execution.
PROGRAM:
//implementation of text editor
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<fstream.h>
#include<stdlib.h>
#include<stdio.h>
char fn[10],c;
class texteditor
{
public:
void create();
void append();
void view();
void del();
};
void main()
{ int choice;
while(1)
{ clrscr();
texteditor obj;
cout<<"\n1.CREATE\n2.VIEW\n3.APPEND\n4.DELETE\n5.EXIT\nENTER YOUR
CHOICE:";
cin>>choice;
switch(choice)
{ case 1:
obj.create();
break;
case 2:
obj.view();
break;
case 3:
obj.append();
break;
case 4:
obj.del();
break;
case 5:
exit(0);
break;
default:
exit(0);
}}} //Creation of file
void texteditor::create()
{ cout<<"\nENTER THE FILE NAME:";
cin>>fn;
ofstream fp1;
fp1.open(fn);
cout<<"\nENTER THE CONTENTS OF THE FILE:";
cout<<"\nSAVE AND EXIT: CTRL &S\n";
while(1)
{ c=getch();
if(c==32)
{ cout<<' ';
fp1<<c;
} else if(c==13)
{ cout<<"\n";
fp1<<"\n";
} else if(c==19)
{ cout<<"\nFILE SAVED AND CLOSED\n";
fp1.close();
getch();
break;
} else
{ cout<<c;
fp1<<c;
}}} //Procedure for viewing the content of file
void texteditor::view()
{ char str[30];
cout<<"\nENTER THE FILE NAME:";
cin>>fn;
ifstream fp1(fn);
while(!fp1.eof())
{
fp1.getline(str,30);
cout<<str<<"\n";
}
fp1.close();
getch();
} //Procedure for appending data to a
file
void texteditor::append()
{ cout<<"\nSAVE:CTRL &
S";
cout<<"\nENTER THE FILE NAME:";
cin>>fn;
ofstream fp1(fn,ios::ate);
while(1)
{ c=getch();
if(c==13)
{ c='\n';
cout<<"\n";
fp1<<c;
} else if(c==19)
{
break;
} else
{ cout<<c;
fp1<<c;
}}
fp1.close();
} //Procedure for deleting a file
void texteditor::del()
{ cout<<"\nENTER THE FILE NAME:";
cin>>fn;
remove(fn);
cout<<"\nFILE DELETED SUCCESSFULLY\n";
getch();
}
OUTPUT:
1.CREATE
2.VIEW
3.APPEND
4.DELETE
5.EXIT
ENTER YOUR CHOICE:1
ENTER THE FILE NAME: ANAND
ENTER THE CONTENTS OF THE FILE:
SAVE AND EXIT: CTRL &S
FRIENDS
FILE SAVED AND CLOSED
ENTER YOUR CHOICE:2
ENTER THE FILE NAME: ANAND
FRIENDS
ENTER YOUR CHOICE:3
SAVE:CTRL &S
ENTER THE FILE NAME:ANAND
JEYRAJ
ENTER YOUR CHOICE:4
ENTER THE FILE NAME:ANAND
FILE DELETED SUCCESSFULLY
ENTER YOUR CHOICE:5
EXIT…
0 comments:
Post a Comment