How to make my text editor better?

Hello,

I'm a beginner in C++ programming and currently writing a text editor/-viewer, which works from its basic functionalities, like opening existing text files into a vector array, outputting them, parsing command line strings and writing new (empty) files.

The only downside is that it only adds information (text) to files, but doesn't allow me to change existing text. Instead of adding text to existing text I would rather always have the existing text from the vector getting parsed into the input field, so the user can change it and also add new stuff.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <term.h>
#include <unistd.h>
#include <iterator>

using namespace std;

int i = 1;

string temp;
std::vector< string > bank;

static int aw = 0;

class Editor {
public:
    void Read (int c);
    void Write (int c);
    void Save (int c);
    void Open (int c);
    void New (int c);
};

void ClearScreen();

void Editor::Read (int c) {
    ClearScreen();
    cout << "(Read mode) " << "i=" << i << endl;
    
    i = 1;
    
    for (std::vector<string>::const_iterator i = bank.begin(); i != bank.end(); ++i) {
        std::cout << *i;
        std::cout << endl;
    }
    
    i =1;
    
    cin >> temp;
    std::cin.clear(); // optional, use when input has gone bad; clears error flags
    ClearScreen();
}

void Editor::Write (int c) {
    ClearScreen();
    cout << "(Write mode)" << endl;

    while (temp != "end" && c=='w') {
        
        getline (cin,temp);
        std::cin.clear(); // optional, use when input has gone bad; clears error flags
        
        if (temp !="end" && c=='w') { (void)bank.insert(bank.end(),temp); }
        
    }
    
    ClearScreen();

}


void Editor::Save (int c) {
    ClearScreen();
    cout << "(Save mode) " << "i=" << i << endl;
    cout << "Where shall I save to?" << endl;
    
    while (temp != "end" && c=='s') {
        
        cin >> temp;
        std::cin.clear(); // optional, use when input has gone bad; clears error flags
        
        ofstream output_file(temp);
        
        if (output_file.is_open())
        {
            ostream_iterator<string>output_iterator(output_file, "\n");
            copy(bank.begin(), bank.end(), output_iterator);
            output_file.close();
            
            temp = "end";
            ClearScreen();
            
        } else cout << "Can't save this. Try again or type 'end'."; }
    
    ClearScreen();
}

void Editor::Open (int c) {
    ClearScreen();
    cout << "(Open mode) " "i=" << i << endl;
    cout << "Which file shall I open?" << endl;
    
    while (temp != "end") {
        
        cin >> temp;
        std::cin.clear(); // optional, use when input has gone bad; clears error flags
        
        ifstream input_file(temp);
        
        if (input_file.is_open())
        {
            std::string s((std::istreambuf_iterator<char>(input_file)), std::istreambuf_iterator<char>());
            (void)bank.insert(bank.end(),s);
            
            input_file.close();
            
            temp = "end";
            aw = 1;
            ClearScreen();
            
        } else cout << "Can't open this. Try again or type 'end'." << endl;
        
    }
    ClearScreen();
}

void Editor::New (int c) {
    ClearScreen();
    cout << "(New mode) " "i=" << i << endl;
    
    vector<string>().swap(bank);
    
    aw = 0;
    ClearScreen();
}

int main(int argc, char* argv[]) {
    
    char c;
    Editor editor1;
    Editor editor2;
    
    if (argc == 2) { // We expect 3 arguments: the program name, the source path and the destination path
        ifstream input_file(argv[1]);
        
        if (input_file.is_open())
        {
            std::string s((std::istreambuf_iterator<char>(input_file)), std::istreambuf_iterator<char>());
            (void)bank.insert(bank.end(),s);
            
            input_file.close();
            
            cout << "(Read mode) " << "i=" << i << endl;
            
            i = 1;
            
            for (std::vector<string>::const_iterator i = bank.begin(); i != bank.end(); ++i) {
                std::cout << *i << ' ' << endl;
            }
            
            i =1;
            
            cin >> temp;
            std::cin.clear(); // optional, use when input has gone bad; clears error flags
            
            
        } else cout << "Can't open this. Try again or type 'end'.";
    }
    
    int pr = 1;
    
    void option();
    
    ClearScreen();
    
    
    do
    {
        option();
        temp = "";
        cin >> c;
        std::cin.clear(); // optional, use when input has gone bad; clears error flags

        if (c=='r' ) {
            editor1.Read(c);
        }
        else if (c=='r' && aw==0) { ClearScreen(); }
        
        else if (c=='9') {
            editor2.Read(c);
        }
        
        else if (c=='w') {
            editor1.Write(c);
        }
        else if (c=='s') {
            editor1.Save(c);
        }
        else if (c=='o') {
            editor1.Open(c);
        }
        else if (c=='0') {
            editor2.Open(c);
        }
        else if (c=='n') {
            editor1.New(c);
            
        } else if (c=='e') { pr=0; }
        
        
    }while (pr==1);
    
    
    return 0;
}

void option() {
    cout << "read? (r) \t add? (w) \t open (o) \t save? (s) \t new? (n)" << endl;
    
}

void ClearScreen()
{
    if (!cur_term)
    {
        int result;
        setupterm( NULL, STDOUT_FILENO, &result );
        if (result <= 0) return;
    }
    
    putp( tigetstr( "clear" ) );
}


Btw: the ClearScreen function is OS specific (OS X) and everything gets successfully compiled with a linker flag (-lncurses) in Xcode utilizing the Apple LLVM 6.0 compiler.

Also, my functions are not dynamic enough to create multiple independent arrays. I'm not sure how to implement this.

I'd be grateful for any help! The first issue (changing existing lines with displaying them) has a higher priority. :)

Thanks in advance!
Topic archived. No new replies allowed.