Problem 1. Text Editor using files and strings using C++ and flowchart The FlowC
ID: 3698136 • Letter: P
Question
Problem 1. Text Editor using files and strings using C++ and flowchart
The FlowCharts
In this project, you should write a simple line editor. Keep the entire text on a file, one line in a key Enter. Start the program with entering EDIT file, after which a prompt appears along with the line number. If the letter I is entered with a number n following it, then insert the text to be followed before line n. If I is not followed by a number, then insert the text before the current line. If D is entered with two numbers n and m, one n, or no number following it, then delete lines n through m, line n, or the current line. Do the same with the command L, which stands for listing lines. If A is entered, then append the text to the existing lines. Entry E signifies exit and saving the text in a file. Here is an Example:
EDIT testfile
1>The first line
2>
3> And another line
4>I3
3> The second line
4> One more line
5> L
1>The first line
2>
3> The second line
4> One more line
5> And another line // This is now line 5, not 3;
5> D2 // line 5, since L was issued from line 5;
4> L //line 4, since one line was deleted;
The first line
2> The second line // this and the following lines
3> One more line // now have new numbers
4> And another line
4> E
Explanation / Answer
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
using namespace std;
const int N=255;
void append(string mas[]);
void del (string mas[]);
void insertN (string mas[]);
int main()
{
int ans;
ifstream file("demoFile.txt");
ofstream file2("DemoFileOp.txt");
file2<<file.rdbuf();
file.close();
file2.close();
do {
string mas[N];
string prompt;
ifstream file;
file.open("DemoFileOp.txt" , ios::in);
int i=0;
while(!file.eof())
{ getline(file, prompt);
mas[i]=prompt;
i++;
}
cout<<" Enter your choice:"<<endl;
cout<<"1.Insert text before n-th row."<<endl;
cout<<"2.Insert text”<<endl;
cout<<"3.Append"<<endl;
cout<<"4.Delete the rows from n-th to m-th."<<endl;
cout<<"5.Exit"<<endl;
cin>>ans;
switch(ans){
case 1: insertN (mas);break;
case 2: insert (mas);break;
case 3: append (mas);break;
case 4: del (mas);break;
case 5: exit(1);
}
}
while(ans!=5);
}
void append (string mas[]){
int n,m;
char app;
cout<<"Please enter the rows you want to be changed !"<<endl;
cout<<"From :";
cin>>n;
n=n-1;
cout<<endl<<"Enter text to append"<<endl;
getline(cin,app);
}
ofstream file2("demoFileOp.txt",ios::in);
for (int i=0 ; i<=N; i++){
file2<< mas[i]<<endl;
}
}
void del (string mas[])
{
int n,m;
cout<<"From :";
cin>>n;
n=n-1;
cout<<endl<<"To :";
cin>>m;
m=m-1;
for ( n ; n<N-n ; n++)
{
m++;
mas[n]=mas[m];
}
ofstream file2("demoFileOpp.txt",ios::in);
for (int i=0 ; i<=N; i++){
file2<< mas[i]<<endl;
}
}
void insertN (string mas[])
{
int n;
string swap[N];
for (int i=0 ; i<N ; i++){
swap[i]=mas[i];
}
cout<<"Please enter the row you want to add text before!"<<endl;
cin>>n;
n=n-1;
cout<<"Enter text for the row : ";
getline (cin, mas[n]);
for ( n ; n<N ; n++){
mas[n+1]=swap[n];
}
ofstream file2("demoFileOp.txt",ios::in);
for (int i=0 ; i<=N; i++){
file2<< mas[i]<<endl;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.