A simple text editor. How??!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 5
Reputation: mrmonkee is an unknown quantity at this point 
Solved Threads: 0
mrmonkee mrmonkee is offline Offline
Newbie Poster

A simple text editor. How??!!

 
0
  #1
Oct 30th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: A simple text editor. How??!!

 
0
  #2
Oct 30th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,187
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 533
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: A simple text editor. How??!!

 
0
  #3
Oct 30th, 2007
you could use a 3d array of characters?
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: A simple text editor. How??!!

 
0
  #4
Oct 30th, 2007
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:
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:
  1. #include <curses.h>
  2.  
  3. int main() {
  4. // Initialize the curses library
  5. initscr();
  6. raw();
  7. (void) noecho();
  8. nonl();
  9. intrflush( stdscr, FALSE );
  10. (void) keypad( stdscr, TRUE );
  11.  
  12. // make sure the cursor is visible
  13. curs_set( 1 );
  14.  
  15. // clear the screen
  16. wclear( stdscr );
  17.  
  18. // move the cursor to (x, y) = (10, 5)
  19. wmove( stdscr, 5, 10 );
  20.  
  21. // insert characters (instead of waddstr())
  22. winsstr( stdscr, "Press the 'Any' key" );
  23.  
  24. // update
  25. wrefresh( stdscr );
  26.  
  27. // wait for user to press a single key
  28. wgetch( stdscr );
  29.  
  30. // all done
  31. endwin();
  32. return EXIT_SUCCESS;
  33. }
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: A simple text editor. How??!!

 
0
  #5
Oct 30th, 2007
Sounds like a pretty retarded exercise to me! And I bet, somehow turbo c is involved!
Last edited by iamthwee; Oct 30th, 2007 at 5:04 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: A simple text editor. How??!!

 
0
  #6
Oct 30th, 2007
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...
Last edited by Duoas; Oct 30th, 2007 at 6:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 5
Reputation: mrmonkee is an unknown quantity at this point 
Solved Threads: 0
mrmonkee mrmonkee is offline Offline
Newbie Poster

Re: A simple text editor. How??!!

 
0
  #7
Nov 4th, 2007
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:
  1. When I see your face, I know that I'm finally yours.
  2. I find a reason, I thought that I lost before.
  3.  
But when I run the program, it shows:
  1. Filename: 1.txt
  2.  
  3. The content:
  4.  
  5. When I see your face, I know that I'm finally yours.════════════════════════════
  6. @¶D═I find a reason, I thought that I lost before.══════════════════════════════
  7. ═══░‼D══════════════════════════════════════════════════════════════════════════
  8. ══════Press any key to continue
  9.  

Please show me what my mistake is!
Thanks you very much!!!
P/S: I'm not good at English

  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <assert.h>
  4.  
  5. typedef struct Node
  6. {
  7. char character[80];
  8. Node *next, *pre;
  9. }Line;
  10.  
  11. Line *currentline;
  12. Line *firstline;
  13. Node *head, *tail;
  14. int col;
  15.  
  16. void createfirstline()
  17. {
  18. Node *p;
  19. p = new Node;
  20. currentline = p;
  21. head = currentline;
  22. tail = currentline;
  23. col = -1;
  24. }
  25.  
  26. void newline()
  27. {
  28. Node *p;
  29. p = new Node;
  30. p -> next = NULL;
  31. p->pre = currentline;
  32. currentline->next = p;
  33. tail = p;
  34. currentline=p;
  35. col = 0;
  36. }
  37.  
  38.  
  39. void createnewline(char x)
  40. {
  41. Node *p;
  42. p = new Node;
  43. p -> next = NULL;
  44. if (head == NULL)
  45. {
  46. head = p;
  47. tail = p;
  48. }
  49. else
  50. {
  51. Node *q = tail;
  52. q->next = p;
  53. p->pre = q;
  54. }
  55. tail = p;
  56. currentline = p;
  57. }
  58.  
  59. void readfile()
  60. {
  61. Node *p;
  62. p = head;
  63. if (head == NULL)
  64. cout << "\nFile is empty!\n" << endl;
  65. else
  66. {
  67. cout << "\nThe content: " << endl << endl;
  68. while (p != NULL)
  69. {
  70. cout << p->character;
  71. p = p->next;
  72. }
  73. }
  74. }
  75.  
  76. void main()
  77. {
  78. cout << "Filename: ";
  79. char filename[30];
  80. cin.getline (filename,30);
  81.  
  82. ifstream instream;
  83. instream.open(filename,ios::nocreate);
  84. if (!instream)
  85. {
  86. cout << "Cannot open the file\n";
  87. }
  88. else
  89. {
  90. char reading;
  91. currentline = firstline;
  92. createfirstline();
  93. while(instream.read(&reading,sizeof(reading)))
  94. {
  95. if (reading == '\n')
  96. newline();
  97. else
  98. {
  99. col++;
  100. currentline->character[col] = reading;
  101. }
  102. }
  103. readfile();
  104. }
  105. instream.close();
  106. }
Last edited by Ancient Dragon; Nov 4th, 2007 at 7:39 am. Reason: add line numbers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: A simple text editor. How??!!

 
0
  #8
Nov 4th, 2007
Honey why don't you tell us what you're using. Is is turbo c?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: A simple text editor. How??!!

 
0
  #9
Nov 4th, 2007
You did not null-terminate the string. After line 102 (end of the loop) add this:
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.

  1. std::string reading;
  2. while( getline(instream, reading) )
  3. {
  4. strcpy( currentline->character, reading.c_str());
  5. }

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: A simple text editor. How??!!

 
0
  #10
Nov 4th, 2007
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.
#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:
  1. int main()
  2. {
  3. // do stuff here
  4. return EXIT_SUCCESS;
  5. }

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...
See what is happening? To read lines from file, instead use something like:
  1. char reading[ 80 ];
  2. instream.getline( reading, 80 );
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC