trying to make a simple console text editor

hi, i'm new here, and i have been trying to make a text editor on c++ since yesterday, because i'm having an issue:
when i open the file, the contents of it are showed, but i can't edit its contents.
here's the code:
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
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
#define clear cout << "\033[H\033[J"
using namespace std;

int main() {
    fstream file;
    string file_name;
    string text;
    cout << "Welcome to core's text editor." << endl;
    cout << "Type the file name here to edit it: ";
    getline (cin, file_name);
    file.open(file_name, ios::in | ios::out);
    if (file.is_open()) {
        clear;
        cout << "[" << file_name << "]" << endl;
        cout << endl;
        char contents;
        while (file.good()) {
            contents = file.get();
            cout << contents;
        }
        getline (cin, text);
        file << text;
        file.close();
    }
    else {
        cout << "Failed to open file!" << endl;
        cin.get();
        main();
    }
}

i'm making this text editor for the operating system i'm gonna make when i get more experienced at c++, and yes, the operating system is called "core" lol. i have been self-teaching myself c++ for a very little while, so i make a lot of mistakes, and the same goes to english lmfao
I see by line 5 that you're using VT100 escape sequences.
You have two choices.

1) Fill the screen with the text to be edited. Let the user modify the screen, then when the user presses a function key, position the cursor to 0,0 and read back the entire screen.

2) Paint the screen as in 1. Track every keystroke the user makes and modify an in memory copy of the screen appropriately.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
using namespace std;

int main () {
	char c;
	string s= "this is the best example of getting booed! o0O";
	fstream file;
	file.open("file 01.txt");	
	if(file.is_open()){	
		cout<< "file opened\n";
		file << s;
		file.seekg(0);
		while(file.good()){
			file >> c;
			if(c=='o'){
				file.seekg(file.tellg()-1);
				file << 'O';
			}
		}			
	}
	
	
}
AbstractionAnon said:

I see by line 5 that you're using VT100 escape sequences.
You have two choices.

1) Fill the screen with the text to be edited. Let the user modify the screen, then when the user presses a function key, position the cursor to 0,0 and read back the entire screen.

2) Paint the screen as in 1. Track every keystroke the user makes and modify an in memory copy of the screen appropriately.

i was thinking about making a mix of these with some extras: print the entire screen with the file contents, let the user modify the screen, track every keystroke the user makes and make some shortcuts with the keyboard (^x, for example) to make it easy. so the user should first type in their contents on the file, and then everything goes fine until the control key is pressed, so i can make a ^x shortcut to quit and save the file, a ^q shortcut to quit without saving a file, and so on.
i gave up on making this text editor, but i'm trying to make my own shell right now. maybe in the future i'll make a better version of this crappy text editor.
you should work ground up
anup30 makes a good point, As you probably discovered, a console window based a text editor is limited by the commands that the console window supports. That's why all the good text editors create their own windows.

The best text editor I ever worked on was on a system where the screen was refreshed directly from memory. You wanted to change something on the screen, you simply changed it in memory which was directly accessible in your program. The screen changed on the next refresh. I forget was the refresh rate was, but it was quite high. Driven by the processor clock.
text editors are one of those things that sound simple but are rather difficult even for a notepad type minimal interface. It would probably take me many days or weeks to make one (not cheating and using a built in tool like window text box widget).
you have to have a way to efficiently move text to insert and delete, copy paste blocks, etc. Details ...
You don't know how lucky you are now - to have a screen editor. Way back when we used teletypes which only had a paper roll for output. Editing was by typing edit commands in the text editor.

eg

this is line 1
this is line 2
this is line 3


to change L3


c3
s
i/this is third line
/


or

c2
d/line/i/new word/

yeah, maybe i should continue my project, no matter how difficult it is to make it. i only need to study and pay more attention to do so.
Writing a simple text screen editor is not that difficult. Ok, it's non-trivial but certainly doable. I wrote two over a summer holiday whilst at Uni - one in Fortran 77 and one in PDP assembler, as the dept was changing from teletypes to vdu screens - and I'm a lazy typist! Over the next few years I ported it to RSTS (Basic/Assembler)and then to Pick (Basic and Assembler) and then to DOS (c/assembler) enhancing it as I went.

However, this is one project where you really should design it before you code - and not just jump straight in with both feet coding. You need to get the structs/classes sorted and the flow of the program/data before coding. Also, what commands/actions you're going to support, which keys are going to do what, are you having a command mode or just have functionality on pressing keys (function keys etc). This all needs to be determined as part of the design. Once you have all this, then you can start doing some high-level coding (code the structs/classes and the functions (without much bodies) and comments where detailed code is going and see how it hangs together as a design. If it does, then start implementing the function bodies etc. Then simple operations (insert/delete etc) and then more complicated ones (replace etc) until you have a functional screen editor.
Last edited on
What I was alluding to earlier was that if you're writing your own OS, you should consider writing your own console window first if you want to do more in your text editor than the VT100 command set supports.
well, i decided to go back to continuing this project, and now i've finally managed to get the cursor to move across the screen. however, i still don't know how to do the main part which is text editing. i tried using getline() inside the loop that makes the cursor move across the screen with the arrow keys, but that didn't work. at least i managed to create a shortcut: esc closes the file and goes back to the menu. what i'm trying to do is make the cursor movement loop and the text edit loop work until the user hits esc, so the user can move the cursor and edit the contents.
Is this to be x-platform or just for windows? You mention your own os?

Usually, command/input for a text editor is obtained 1 key at a time. Get key, process etc. In standard C++ there's no facility to obtain a key/char until a new-line has been entered. For Windows you'd use _getch() in conio.h (or the Windows console functions) - but this is non-portable. If you're writing your own os, then i/o is going to a part of that.

i'm actually making this project to rewrite it in assembly in the future, so i can include it in my own operating system, which will be entirely written in assembly. and yeah, at the moment it will only work on windows, although i was thinking about making it cross-platform.
well, i decided to go back to continuing this project, and now i've finally managed to get the cursor to move across the screen. however, i still don't know how to do the main part which is text editing. i tried using getline() inside the loop that makes the cursor move across the screen with the arrow keys, but that didn't work. at least i managed to create a shortcut: esc closes the file and goes back to the menu. what i'm trying to do is make the cursor movement loop and the text edit loop work until the user hits esc, so the user can move the cursor and edit the contents.

one thing i should do is create a limit for the cursor not to move on the first 2 lines, because these lines will show the text information, eg file name, whether it has been modified or not, etc.
i think i should post my current code here:
https://pastebin.com/6WEfs58q
You could be using Microsoft's low-level direct-to-console interface and not virtual terminal codes:
https://docs.microsoft.com/en-us/windows/console/low-level-console-i-o

The interface allows you to consider the console screen buffer as a block of memory, essentially as @AbstractionAnon describes above.
yeah.
Topic archived. No new replies allowed.