| | |
A simple text editor. How??!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Solved Threads: 0
I have an exercise like this:
Design a simple text editor in console mode, not window form
max character in a line is 80
user can move cursor up, down, to left and right, insert, delete character
I don't know how to build it
i should use linked list or stack? and how to move the cursor, and ... lots of things i don't know
some one please help me!
Design a simple text editor in console mode, not window form
max character in a line is 80
user can move cursor up, down, to left and right, insert, delete character
I don't know how to build it

i should use linked list or stack? and how to move the cursor, and ... lots of things i don't know
some one please help me!
Not possible to do it in pure C or c++ because the languages do not support cursor movement. You could do it in MS-DOS pretty easily with TurboC++ compiler. All modern MS-Windows compilers will require win32 api console functions. For *nix I suppose you might have to use curses library functions.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
interfacing with the console
If on Windows you'll have to use the (non-standard) stuff in <conio.h>. Just include the library file and compile.
If on linux you'll have to use the curses library. There are various versions of it: curses, ncurses, and pdcurses. You are sure to have at least one of them. To compile a program using the curses library you need to do something like:
(assuming "pdcurses", of course). If you have more than one version of curses, choose pdcurses over ncurses over curses... In all cases, include the file <curses.h> in your code. Get documentation on the internet, or at the school terminal prompt by typing "man curses".
Don't try to get around curses. Raw input from the keyboard is not always friendly. The curses library makes it friendly.
memory layout
There are two common ways to store textual data. I recommend you stick with a linked list of lines. That is how vi does it. (The other method is to have the entire file stored in one giant buffer, and the stuff on screen copied over into a smaller editing buffer. This is how emacs does it. There is nothing wrong with this method, but it requires some careful bookkeeping you can avoid for a simple assignment.)
example
Here is a simple example for you:
For an editor, you will usually want to use winstr() to insert characters in a line instead of waddstr() which overwrites...
Other useful functions are: wdelch(), winsdelln(), getyx(), and winsch().
Well, that should be enough to get you started.
Good luck.
If on Windows you'll have to use the (non-standard) stuff in <conio.h>. Just include the library file and compile.
If on linux you'll have to use the curses library. There are various versions of it: curses, ncurses, and pdcurses. You are sure to have at least one of them. To compile a program using the curses library you need to do something like:
g++ -o myprog myprog.cpp -lpdcurses(assuming "pdcurses", of course). If you have more than one version of curses, choose pdcurses over ncurses over curses... In all cases, include the file <curses.h> in your code. Get documentation on the internet, or at the school terminal prompt by typing "man curses".
Don't try to get around curses. Raw input from the keyboard is not always friendly. The curses library makes it friendly.
memory layout
There are two common ways to store textual data. I recommend you stick with a linked list of lines. That is how vi does it. (The other method is to have the entire file stored in one giant buffer, and the stuff on screen copied over into a smaller editing buffer. This is how emacs does it. There is nothing wrong with this method, but it requires some careful bookkeeping you can avoid for a simple assignment.)
example
Here is a simple example for you:
C++ Syntax (Toggle Plain Text)
#include <curses.h> int main() { // Initialize the curses library initscr(); raw(); (void) noecho(); nonl(); intrflush( stdscr, FALSE ); (void) keypad( stdscr, TRUE ); // make sure the cursor is visible curs_set( 1 ); // clear the screen wclear( stdscr ); // move the cursor to (x, y) = (10, 5) wmove( stdscr, 5, 10 ); // insert characters (instead of waddstr()) winsstr( stdscr, "Press the 'Any' key" ); // update wrefresh( stdscr ); // wait for user to press a single key wgetch( stdscr ); // all done endwin(); return EXIT_SUCCESS; }
Other useful functions are: wdelch(), winsdelln(), getyx(), and winsch().
Well, that should be enough to get you started.
Good luck.
If you are using the (ancient, obsolete) TurboC, then you have access to functions like clrscr(), and gotoxy(). With other windows compilers, though, you are unlikely to have them... The Wikipedia article lists all the functions from conio.h you are likely to have on any given windows compiler. If you are using an MS compiler, you might have something like _clearscreen() or somesuch...
You could, like Ancient Dragon said, play with the Windows Console functions, which aren't that difficult (SetConsoleCursorPosition()), but require a bit of reading.
I still recommend using pdcurses. It is truly cross-platform and avoids having to play with callback hooks...
[EDIT]
Actually, now that I'm awake... Hasn't your professor given you any instruction on how to interface with the console? Seems an odd assignment without giving you some instructions on how to read arrow key presses and position the cursor on the screen...
You could, like Ancient Dragon said, play with the Windows Console functions, which aren't that difficult (SetConsoleCursorPosition()), but require a bit of reading.
I still recommend using pdcurses. It is truly cross-platform and avoids having to play with callback hooks...
[EDIT]
Actually, now that I'm awake... Hasn't your professor given you any instruction on how to interface with the console? Seems an odd assignment without giving you some instructions on how to read arrow key presses and position the cursor on the screen...
Last edited by Duoas; Oct 30th, 2007 at 6:38 pm.
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Solved Threads: 0
Thank you very much!
I'm just a beginner in C++ so something you said I don't understand clearly.
This is my simple program, I use double linked list, the program opens a text file, and show the file's content. (I will add the cursor movement later)
My file's content:
But when I run the program, it shows:
Please show me what my mistake is!
Thanks you very much!!!
P/S: I'm not good at English
I'm just a beginner in C++ so something you said I don't understand clearly.
This is my simple program, I use double linked list, the program opens a text file, and show the file's content. (I will add the cursor movement later)
My file's content:
C++ Syntax (Toggle Plain Text)
When I see your face, I know that I'm finally yours. I find a reason, I thought that I lost before.
C++ Syntax (Toggle Plain Text)
Filename: 1.txt The content: When I see your face, I know that I'm finally yours.════════════════════════════ @¶D═I find a reason, I thought that I lost before.══════════════════════════════ ═══░‼D══════════════════════════════════════════════════════════════════════════ ══════Press any key to continue
Please show me what my mistake is!
Thanks you very much!!!
P/S: I'm not good at English

C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <fstream.h> #include <assert.h> typedef struct Node { char character[80]; Node *next, *pre; }Line; Line *currentline; Line *firstline; Node *head, *tail; int col; void createfirstline() { Node *p; p = new Node; currentline = p; head = currentline; tail = currentline; col = -1; } void newline() { Node *p; p = new Node; p -> next = NULL; p->pre = currentline; currentline->next = p; tail = p; currentline=p; col = 0; } void createnewline(char x) { Node *p; p = new Node; p -> next = NULL; if (head == NULL) { head = p; tail = p; } else { Node *q = tail; q->next = p; p->pre = q; } tail = p; currentline = p; } void readfile() { Node *p; p = head; if (head == NULL) cout << "\nFile is empty!\n" << endl; else { cout << "\nThe content: " << endl << endl; while (p != NULL) { cout << p->character; p = p->next; } } } void main() { cout << "Filename: "; char filename[30]; cin.getline (filename,30); ifstream instream; instream.open(filename,ios::nocreate); if (!instream) { cout << "Cannot open the file\n"; } else { char reading; currentline = firstline; createfirstline(); while(instream.read(&reading,sizeof(reading))) { if (reading == '\n') newline(); else { col++; currentline->character[col] = reading; } } readfile(); } instream.close(); }
Last edited by Ancient Dragon; Nov 4th, 2007 at 7:39 am. Reason: add line numbers
You did not null-terminate the string. After line 102 (end of the loop) add this:
Or, even easier, use a std::string object for file input and getline() to read every line in the file. If you do that then you don't need to read the file one character at a time or worry about the '\n' character because getline() will do that for you.
What is readfile on line 59 supposed to do? Did you name that function correctly? As currently written all it does is diaplay the text that was read from main().
currentline->character[col] = 0; Or, even easier, use a std::string object for file input and getline() to read every line in the file. If you do that then you don't need to read the file one character at a time or worry about the '\n' character because getline() will do that for you.
C++ Syntax (Toggle Plain Text)
std::string reading; while( getline(instream, reading) ) { strcpy( currentline->character, reading.c_str()); }
What is readfile on line 59 supposed to do? Did you name that function correctly? As currently written all it does is diaplay the text that was read from main().
Last edited by Ancient Dragon; Nov 4th, 2007 at 7:44 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
With the kind of syntax errors it is letting him get away with it almost undoubtedly is TurboC...
Don't use C style headers. Use instead C++ headers.
Personally, I can't stand all the assert stuff people throw in their code for minor errors... but seeing as you don't use any you don't need to include the header...
The main() function always returns an int. At the very least it should look like this:
The ifstream class does not need to be told not to create a file. (But ios::nocreate is non-standard anyway.) Just say:
OK, on to logic errors.
First, you need to be much more careful how you handle your linked list nodes. You've got dangling pointers.
Use function arguments to create a node, not global variables. This will help fix your errors. For example, create a function named:
All this function should do is create a new node and fill it with the information given to it.
This forces you to separate your linked list code from handling global variables. To create the first node in the list, just say
You can append a node in a similar way:
The reason your output is full of funny characters is because you have not initialized your char[80] string properly. This is because you are reading until you get to the end of line but otherwise not terminating your string or clearing it of characters already there. A slight "animation", as it were:
See what is happening? To read lines from file, instead use something like:
This will read a maximum of 79 characters into a reading string and guarantee that it is null-terminated. You can then copy this string into a node using add_node().
Hope this helps.
Don't use C style headers. Use instead C++ headers.
#include <iostream>#include <fstream>#include <cassert>Personally, I can't stand all the assert stuff people throw in their code for minor errors... but seeing as you don't use any you don't need to include the header...
The main() function always returns an int. At the very least it should look like this:
C++ Syntax (Toggle Plain Text)
int main() { // do stuff here return EXIT_SUCCESS; }
The ifstream class does not need to be told not to create a file. (But ios::nocreate is non-standard anyway.) Just say:
instream.open( filename );OK, on to logic errors.
First, you need to be much more careful how you handle your linked list nodes. You've got dangling pointers.
Use function arguments to create a node, not global variables. This will help fix your errors. For example, create a function named:
Line *add_node( char *text, Line *prev, Line *next );All this function should do is create a new node and fill it with the information given to it.
This forces you to separate your linked list code from handling global variables. To create the first node in the list, just say
head = tail = add_node( "I am first", NULL, NULL );You can append a node in a similar way:
tail = add_node( "I am not first", tail->prev, NULL );The reason your output is full of funny characters is because you have not initialized your char[80] string properly. This is because you are reading until you get to the end of line but otherwise not terminating your string or clearing it of characters already there. A slight "animation", as it were:
xy--ze===gobbledegook...ze (line as it is when you start the program) Wy--ze===gobbledegook...ze Wh--ze===gobbledegook...ze Whe-ze===gobbledegook...ze Whenze===gobbledegook...ze When e===gobbledegook...ze When I===gobbledegook...ze When I ==gobbledegook...ze When I s=gobbledegook...ze etc...
C++ Syntax (Toggle Plain Text)
char reading[ 80 ]; instream.getline( reading, 80 );
Hope this helps.
![]() |
Similar Threads
- asp .net & vb .net html rich text editor (ASP.NET)
- Java Text Editor (Scroll Bars) (Java)
- text editor (C++)
- undo - redo function in a text editor (Java)
- Hotmail Rich Text Editor (Windows NT / 2000 / XP)
- Best Java Program Editor? (Java)
- edlin text editor (C++)
- simple text to MP3 (Graphics and Multimedia)
- need help in creating simple line editor (C++)
Other Threads in the C++ Forum
- Previous Thread: help needed on a loop again
- Next Thread: Dynamic buffer allocation
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






