944,198 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 11476
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 30th, 2007
0

A simple text editor. How??!!

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mrmonkee is offline Offline
5 posts
since Oct 2007
Oct 30th, 2007
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 30th, 2007
0

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

you could use a 3d array of characters?
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,534 posts
since Apr 2005
Oct 30th, 2007
0

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

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:
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Oct 30th, 2007
0

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

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 30th, 2007
0

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

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 4th, 2007
0

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

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mrmonkee is offline Offline
5 posts
since Oct 2007
Nov 4th, 2007
0

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

Honey why don't you tell us what you're using. Is is turbo c?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 4th, 2007
0

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

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.

C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 4th, 2007
0

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

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help needed on a loop again
Next Thread in C++ Forum Timeline: Dynamic buffer allocation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC