Thread: developing a simple Text editor in C

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    6

    developing a simple Text editor in C

    Hi all,

    I am pretty new to c programming. At present, I have a taken up a project of developing a Simple Text Editor using C.

    But I couldnt visualize, how a text editor could be programmed in C.

    In a text editor program, user should be able to edit the text in the screen directly. User should be able to use ARROW KEYS / BACKSPACE / DELETE key etc.

    I would be of a great help if someone could help me with some basic idea/algorithm required for developing the text editor like

    1. How to accept keys from user.
    2. How to display the characters accordingly.

    Thanks
    rssrik

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you're new to C programming, I would not attempt it, yet.

    I would not recommend one write a book of poetry or write a novel, before gaining a strong command of English, or some other language that they would be writing in. Similarly, learn C and get very good at it, before trying something large. Something like a text editor in C is more advanced than you might think it to be.

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by rssrik View Post
    I would be of a great help if someone could help me with some basic idea/algorithm required for developing the text editor like

    1. How to accept keys from user.
    2. How to display the characters accordingly.
    Those are trivial questions that require looking at documentation to find the answer. The more interesting question is, what data structure are you going to use to represent the opened text file in relation to its position on screen and the position of the cursor?
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    6
    Thanks for reminding me that I am a beginner.

    But I am not a absolete "Hello worlder". I think I can handle it. Only problem is that I dont know where to start !!!

    I will appeciate If you could give me some basic ideas. (i'm not requesting for code)

    Thanks again,
    rssrik

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Looks like you have identified the problematic parts of writing a text editor.
    Trouble is that standard c doesnt support any of this functions. So you will have to use external libraries ( like ncurses ) or use OS functions.
    Kurt

  6. #6
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    Here is a link which may be of use - Lnote. It is a text editor written in gtk+. You could download the source and have a look at it, it may not be of any use to you though as it may require a fair bit knowledge to understand. In any case, here is a gtk+ 2.0 tutorial to get you started.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can write a very simple text editor if you're using Dev-C++ under Windows with getche() from the non-standard <conio.h>; getch() might work too with new versions of Dev-C++. What's the difference? http://www.daniweb.com/forums/thread37195.html

    If you want to have a cursor-moving function you can use gotoxy(), also in <conio.h>. An implementation of it for Windows compilers can be found here: http://forums.devshed.com/c-programm...ct-149226.html

    That's if you don't want to use ncurses or another library. We don't really know your level of programming ability so I don't know what to recommend.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    6
    Thanks Zuk & DwkS,

    I read the NCURSES LIB and it seems to hav most of the tools for developing an editor.
    I think it is portable too !

    ---------------------------------------
    gcc (rssrik).c libncurses.a

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You know that you should link with libraries with -lncurses (for libncurses.a)?

    Yes, ncurses is definitely a better way than using getch() etc, but it's a bit harder.

    Anyway, good luck.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Jun 2007
    Posts
    6

    Thumbs up

    libncurses.a is the NCURSES library to get linked with the code !

    I am using "Linked List" to back the representation in screen. The default screen has 25 lines.
    I need to know, How many lines should I BUFFER in memory to represent the screen ?

    1. Suppose If I BUFFER only the 25 lines to be displayed in screen, then for every "PAGE_UP/PAGEDOWN/UPARROW/DOWNARROW" operation, I will have to read the required lines from the file and display it.

    I feel these instructions would take few secs.

    2. If I BUFFER 5-6 pages in memory itself, then i FEEL that precious memory is wasted.

    Could someone justify, which is best method.

    Thanks
    gcc -lncurses rssrik.c

    _____________________________________________

    Everyone Dream of Success, BUT
    Those who succeed, will wake up and work for it !!!
    Have You ?
    Last edited by rssrik; 07-02-2007 at 10:22 PM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > then i FEEL that precious memory is wasted.
    "precious"?
    How many MB or GB of memory do you have?

    Sure, if your text file is many MB in size you might want to take some kind of paging approach on a grand scale, but anything less than 1MB should be entirely in memory IMO.

    > I will have to read the required lines from the file and display it.
    Just wait for all the complaints about your editor being so damn slow.
    Reading the disk every key press is not the way to go.

    > How many lines should I BUFFER in memory to represent the screen ?
    Zero perhaps?
    http://en.wikipedia.org/wiki/Model_view_controller
    Make nice clear boundaries between input, edit actions and output. The core editor itself for example shouldn't give a hoot where the input is really coming from (the user or a macro replay), or where the output is going (ncurses, win32 GUI, etc).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  2. Simple Text manipulation program
    By phlavio in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2003, 12:35 PM
  3. Replies: 5
    Last Post: 02-01-2003, 10:58 AM
  4. text simple question
    By Unregistered in forum Game Programming
    Replies: 2
    Last Post: 04-26-2002, 09:45 AM
  5. simple text dialog ok cancel
    By Brian in forum Windows Programming
    Replies: 1
    Last Post: 02-12-2002, 02:20 AM