Thread: Text editor?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65

    Text editor?

    How do you make C edit text? As in run the cursor over it and change it?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is a very short question with a very long answer. To write a VERY simple editor is about 300 lines of code, I expect. [In fact, I did one as a "challenge" not so long ago - it was 274 lines in the main file, and a linked-list handling file of another 108 lines - along with one header-file of a few lines of prototypes copied from the linked-list source].

    Of course, that is to load a text-file, edit it in some simple ways (delete line, add line), and save it back out again.

    To "mark" text by for example holding shift and using the arrow-keys will involve some form of system-dependant code (or a system-independent library that does the system-dependant parts FOR YOU).

    Of course, if we are not talking console, but a full Windows or Xwindows application, then it is both simpler and harder, as some things are done for you, but you need to write more code in other places simply to achieve a window on the screen, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You need a setting for the text on screen (which is where the cursor is). This is probably WAY more complicated than you might have hoped. After all, barking up this tree won't really cut it:
    Code:
    #include <stdio.h>
    
    int main() {
    	printf("XX XX\b\b\b\bY");
    }
    Have a look into ncurses() or an actual GUI library.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    I don't want a GUI, and I don't care how long it takes, please just tell me how.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://www.adrianxw.dk/SoftwareSite/index.html
    http://pdcurses.sourceforge.net/
    Read both, and learn about controlling the console screen.
    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.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Do you have any links for controlling the console screen?

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sly View Post
    I don't want a GUI, and I don't care how long it takes, please just tell me how.
    You can't. I believe what you want to do and what matsp was talking about are not quite the same thing -- his program did not allow you to back the cursor up the screen and actually delete and replace something on the screen. Or I will eat an egg.

    ncurses() runs in the console (ie, from the command-line); a lot of old pre-net era applications were that way. It's easier to use than a GUI API, but it will still take you awhile. There are some tutorials around. ncurses() is actually quite neat.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    So it is either a GUI or ncurses?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    You can't. I believe what you want to do and what matsp was talking about are not quite the same thing -- his program did not allow you to back the cursor up the screen and actually delete and replace something on the screen. Or I will eat an egg.
    No eggs needed here.

    I have written code to walk about on the screen for various purposes (including implementig an Emacs-like editor written in PDP-11 assembler on 68K). But just handling the keyboard/screen in Windows or Linux is a major undertaking - I'd expect that to be at least 2-300 lines of code just to be able to put some text up and move about within it. That doesn't include any actual EDITING - that will add even more code. I counted up some 380 lines of code to do the other basic bits, and that's with a fair bit missing for editing a line of text in the way you can in DOS's "EDIT".

    Yes, it can be done - and you can either use some curses version (pd or n) or you can write your own functions to move within the text on screen yourself. But if you have never done it before, you will probably end up spendig QUITE A LOT of time just doing that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    You haven't given me any idea how to write my own functions to do that.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sly View Post
    You haven't given me any idea how to write my own functions to do that.
    What function(s) do you want me to give you ideas for? If you are using windows, you need to interface to the Console Functions. There are some of that in the FAQ, the rest is on the MSDN site http://msdn.microsoft.com/en-us/libr...73(VS.85).aspx

    I'm sorry, but this forum isn't the "post a few lines of question and get 2-300 lines of working code" - you must have got us confused.

    If you are using Linux/Unix, you need to use ANSI escape sequences (if you need this, try googling for "wiki ansi escape sequence" and try the "I'm feeling lucky" button).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sly View Post
    You haven't given me any idea how to write my own functions to do that.
    The point is that the easiest option is a curses library. This is why they exist. You could write something with #include <ncurses.h> and have user controlled text editing in a few hundred lines or so. The first thing to get a grip on is it uses a "main loop"...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Thanks for the Microsoft link.

  14. #14
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Would it be easier in x86 assembly?

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sly View Post
    Would it be easier in x86 assembly?
    for who?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. text editor
    By akki in forum C Programming
    Replies: 5
    Last Post: 07-11-2009, 02:05 PM
  3. C++ For Text Editor?
    By bmroyer in forum C++ Programming
    Replies: 12
    Last Post: 04-05-2005, 02:17 AM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. help about text editor
    By nag in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 11:45 AM