Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Task Write a C++ program that implements a simple command-oriented text editor.

ID: 3845056 • Letter: T

Question

Task Write a C++ program that implements a simple command-oriented text editor. The program reads commands from its standard input that modify the text of the document. When the program reaches end-of-file on its input, it writes the current contents of the document to standard output and exits Background In addition to the text of the document, the editor maintains a current position indicator, referred to as point, that defines the location for text-modifying commands. Conceptually, point is interpreted as specifying the location between characters rather than the characters themselves. Thus its smallest value specifies the location before the first character in the document, and its largest value specifies the location after the lastcharacter Some commands operate on whole lines. In such cases, the line containing point is the location of the action, and the contents of the line is considered to consist of all of the characters that make up the text (which could be nothing if the line is empty) plus an end-of-line indicator. The lowest value of point for a given line is the location before the first character, the highest value is after the last character but before the end-of-line indicator Input commands that begin with a dot define operations on the document, such as moving point inserting and deleting text, and searching. All other input defines text to be inserted as-is. For example the input of our discontent. move to beginning of document Now is the summer move to previous line SS ummer search for "summer 6d. delete 6 characters iwinter insert "winter" constructs the document Now is the winter of our discontent. Hints A central issue in your design will be the data structure you use to represent the document contents. Two aspects that you might like to consider are whether or not to explicitly represent lines of text as separate entities, and how to store the characters themselves. Representing lines explicitly would simplify the implementation of operations that work on whole lines but will make operations that span lines more difficult. Conversely, if the entire document is a single block of text then line-spanning operations would be simpler, but you would need to explicitly locate line breaks when performing whole-of-line operations. Possible choices for representing the character values include an array of characters, a vector or list of characters, or a string object. An array has minimal space overhead and is compatible ith standard c-string processing functions, but is less convenient and less efficient for insert and delete operations. A vector or list would simplify the insert and delete, but has memory overheads and depends on the STL iterator mechanisms. A string object is convenient but can only be manipulated through the string API

Explanation / Answer

#include<stdio.h>
#include<conio.h>
#include<process.h>
int i,j,ec,fg,ec2;
char fn[20],e,c;
FILE *fp1,*fp2,*fp;
void Compose();
void Insert();
void Delete();
void Output();
void main()
{
do {
clrscr();
printf(" ***** TEXT EDITOR *****");
printf(" MENU: ----- ");
printf(" 1.Compose 2.Output 3.Insert 4.DELETE 5.EXIT ");
printf(" Enter your choice: ");
scanf("%d",&ec);
switch(ec)
{
case 1:
Compose();
break;
case 2:
Output();
break;
case 3:
Insert();
break;
case 4:
Delete();
break;
case 5:
exit(0);
}
}while(1);
}
void Compose()
{
fp1=fopen("temp.txt","w");
printf(" Enter the text and press '.' to save ");
while(1)
{
c=getchar();
fputc(c,fp1);
if(c == '.')
{
fclose(fp1);
printf(" Enter then new filename: ");
scanf("%s",fn);
fp1=fopen("temp.txt","r");
fp2=fopen(fn,"w");
while(!feof(fp1))
{
c=getc(fp1);
putc(c,fp2);
}
fclose(fp2);
break;
}}
}
void Output()
{
printf(" Enter the file name: ");
scanf("%s",fn);
fp1=fopen(fn,"r");
if(fp1==NULL)
{
printf(" File not found!");
goto end1;
}
while(!feof(fp1))
{
c=getc(fp1);
printf("%c",c);
}
end1:
fclose(fp1);
printf(" Press any key to continue...");
getch();
}
void Delete()
{
printf(" Enter the file name: ");
scanf("%s",fn);
fp1=fopen(fn,"r");
if(fp1==NULL)
{
printf(" File not found!");
goto end2;
}
fclose(fp1);
if(remove(fn)==0)
{
printf(" File has been deleted successfully!");
goto end2;
}
else
printf(" Error! ");
end2: printf(" Press any key to continue...");
getch();
}
void Insert()
{
printf(" Enter the file name: ");
scanf("%s",fn);
fp1=fopen(fn,"r");
if(fp1==NULL)
{
printf(" File not found!");
goto end3;
}
while(!feof(fp1))
{
c=getc(fp1);
printf("%c",c);
}
fclose(fp1);
printf(" Type the text and press 'Ctrl+S' to Insert. ");
fp1=fopen(fn,"a");
while(1)
{
c=getch();
if(c==19)
goto end3;
if(c==13)
{
c=' ';
printf(" ");
fputc(c,fp1);
}
else
{
printf("%c",c);
fputc(c,fp1);
}
}
end3: fclose(fp1);
getch();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote