How to edit a text file ?

Discussion in 'C++' started by Chrisshia, Mar 31, 2013.

  1. Chrisshia

    Chrisshia Guest

    i wanna build a text editor,and now idk how to editor a file when i open..for example..if i use cout to show the text's content..and ill cannot to move my cursor to the content and edit...
    Chrisshia, Mar 31, 2013
    #1
    1. Advertising

  2. Chrisshia

    Chrisshia Guest

    im sry for the question is hazy,and the platform is Linux,ill try QT to do it.ill try my best to describe questions clearly..thanks for your blunt and suggstion.
    Chrisshia, Mar 31, 2013
    #2
    1. Advertising

  3. Chrisshia

    Stefan Ram Guest

    Sam <> writes:
    >It took me about a year to write a fully functional text
    >editor component


    It does not take a year to write a text editor with an
    interface that is akin to EDLIN of MS-DOS or ED/EX for
    UNIX and only some basic fatures, it can even be written
    in portable C++!

    However, one needs at least a medium level of C++
    competence.

    One can start to write such an editor with a careful
    separation of the model component (which I call the »edit
    engine«) and the UI component.

    The initial edit engine does not need to use fancy data
    structures like gap buffers or ropes, just a single C++
    ::std::string. But it has an interface (a pure abstract
    class in C++).

    One can have a working editor in a day (a simple text
    command UI for modifications to a single instance of
    ::std::string).

    With regular expressions in C++11, it is more simple to
    implement RE find and replace operations (I guess, I
    have not tried C++11's regular expressions yet).
    The better support for UTF-8 in C++11 also might help.

    More operations can be added from day to day as seen fit.

    Eventually, it is possible to add fancy editor data
    structure like gap buffers (when ::std::string was observed
    to be too slow) or a GUI, without changing the interface of
    the edit engine.
    Stefan Ram, Mar 31, 2013
    #3
  4. Chrisshia

    Stefan Ram Guest

    -berlin.de (Stefan Ram) writes:
    >With regular expressions in C++11, it is more simple to
    >implement RE find and replace operations (I guess, I
    >have not tried C++11's regular expressions yet).
    >The better support for UTF-8 in C++11 also might help.


    When I tried to write a regular-expression
    search-and-replace in C using a regular-expresion library
    (was it called »perlre«?), the hardest part surely was
    memory management: Knowing how large a buffer to allocate
    for the result. I wonder whether C++11 regular expression
    have some features to help the programmer in this regard,
    the easiest thing would be a ::std::string member function
    »replace« that just handles all memory managment itself.

    In the end, for my C project, I found and used a free
    third-party regular-expression library that has a replace
    function which includes the necessary malloc.
    Stefan Ram, Mar 31, 2013
    #4
  5. Chrisshia

    James Kanze Guest

    On Sunday, March 31, 2013 5:20:03 AM UTC+1, Sam wrote:
    > Chrisshia writes:


    > The answer to a question like that is going to be very different for every
    > platform in existence.


    Not really. If all she or he is trying to do is an editor which
    runs in a console window, ncurses will provide a portable
    abstraction. (Whether there's any point in writing such an
    editor is a different question. Although I still occasionally
    use vim in a console.)

    --
    James
    James Kanze, Mar 31, 2013
    #5
  6. Chrisshia

    James Kanze Guest

    On Sunday, March 31, 2013 9:56:51 AM UTC+1, Stefan Ram wrote:
    > Sam <> writes:


    > >It took me about a year to write a fully functional text
    > >editor component


    > It does not take a year to write a text editor with an
    > interface that is akin to EDLIN of MS-DOS or ED/EX for
    > UNIX and only some basic fatures, it can even be written
    > in portable C++!


    A year seemed long to me, too, but he doesn't give the details
    of what the editor actually did, and what its interface looked
    like.

    There's a simple editor at the end of _Programming Tools in
    Pascal_, by Kernighan and Plauger. The book is very dated (as
    one might suspect by Pascal), but the basic principles still
    apply. Doing it in C++ would be even simpler---a month at the
    most.

    But since the OP asked about cursor positionning, I'd guess
    they're looking for more than EDLIN at the user interface.

    > However, one needs at least a medium level of C++
    > competence.


    > One can start to write such an editor with a careful
    > separation of the model component (which I call the »edit
    > engine«) and the UI component.


    That's the key. In the end, if I were to write an editor, it
    would consist of an editing engine, and a set of front ends. In

    > The initial edit engine does not need to use fancy data
    > structures like gap buffers or ropes, just a single C++
    > ::std::string. But it has an interface (a pure abstract
    > class in C++).


    I'd probably use std::vector<char> (or
    std::vector<uint_least32_t> for Unicode). And there's probably
    no need for it to be an abstract class; I think it's probably
    acceptable to have to recompile everything if you change the
    type of the data container.

    > One can have a working editor in a day (a simple text
    > command UI for modifications to a single instance of
    > ::std::string).


    A day, I don't believe, unless you extremely limit the commands.
    IMHO, it will take more than a day just to define the commands
    and write the user documentation, and you can't realistically
    start writing the C++ before you've done that.

    > With regular expressions in C++11, it is more simple to
    > implement RE find and replace operations (I guess, I
    > have not tried C++11's regular expressions yet).
    > The better support for UTF-8 in C++11 also might help.


    > More operations can be added from day to day as seen fit.


    > Eventually, it is possible to add fancy editor data
    > structure like gap buffers (when ::std::string was observed
    > to be too slow) or a GUI, without changing the interface of
    > the edit engine.


    Interestingly enough, one person I know who timed it found that
    insert in a std::vector<char> was largely fast enough, and that
    there was no need for a gap buffer. The machines of the day
    (this was about 20 years ago) could shift the data reasonable
    fast. (On the other hand, you'll probably get better locality
    with a gap buffer. But the operations which will really take
    time will be things like global search and replace, and they
    probably work better with a simple, linear buffer.)

    Another alternative which might make sense is to organize the
    buffer into lines (like vi did, for example). In this case, the
    goal isn't performance (although inserting lines will be
    faster, and global search and replace will probably result in
    moving a lot less data as well). For some instructions (e.g
    cursor down), it's simply a lot easier to implement.

    --
    James
    James Kanze, Mar 31, 2013
    #6
  7. Chrisshia

    Rui Maciel Guest

    Sam wrote:

    > Sorry to be very blunt, and specific, but you will not be able to write
    > some kind of a text editor for a long, long time.


    This isn't exactly true. Today's GUI toolkits offer text editor widgets
    which already pack a lot of features, some even including undo history.
    This means that once someone is able to write a "hello world"-like app for a
    given GUI toolkit and also learn how to read from/write to a file, that
    someone will be able to write a text editor. A very simple and rudimentary
    text editor, but a text editor nonetheless.


    Rui Maciel
    Rui Maciel, Mar 31, 2013
    #7
  8. Chrisshia

    Luca Risolia Guest

    On 31/03/2013 05:18, Chrisshia wrote:
    > i wanna build a text editor,and now idk how to editor a file when i
    > open..for example..if i use cout to show the text's content..and ill
    > cannot to move my cursor to the content and edit...
    >


    Here is how to write a simple text editor with FLTK:

    http://www.fltk.org/doc-1.3/editor.html
    Luca Risolia, Mar 31, 2013
    #8

  9. >
    > There's a simple editor at the end of _Programming Tools in
    >
    > Pascal_, by Kernighan and Plauger. The book is very dated (as
    >
    > one might suspect by Pascal), but the basic principles still
    >
    > apply. Doing it in C++ would be even simpler---a month at the
    >
    > most.
    >
    >

    In my experiments of training novices who learned object pascal
    in 2 to 3 months to write a text editor with a good cad
    system. Only 2 to 4 persons out of 10 can work out the
    drill even step by step hints were given to all testers
    before their hands on the computers within 3 hours.
    88888 Dihedral, Apr 1, 2013
    #9
  10. Chrisshia

    James Kanze Guest

    On Sunday, March 31, 2013 11:00:46 PM UTC+1, Luca Risolia wrote:
    > On 31/03/2013 05:18, Chrisshia wrote:
    > > i wanna build a text editor,and now idk how to editor a file when i
    > > open..for example..if i use cout to show the text's content..and ill
    > > cannot to move my cursor to the content and edit...


    > Here is how to write a simple text editor with FLTK:


    > http://www.fltk.org/doc-1.3/editor.html


    That's more or less how *not* to write an editor. It starts at
    the wrong end, and then gets lost in details before having
    decided the generalities. (To be fair to the page: it looks to
    me as though the "editor" was more of an excuse. That the
    tutorial was about how to use FLTK, not how to write an editor
    from scratch.)

    --
    James
    James Kanze, Apr 1, 2013
    #10
  11. Chrisshia

    Luca Risolia Guest

    On 01/04/2013 18:26, James Kanze wrote:
    > On Sunday, March 31, 2013 11:00:46 PM UTC+1, Luca Risolia wrote:
    >> On 31/03/2013 05:18, Chrisshia wrote:
    >>> i wanna build a text editor,and now idk how to editor a file when i
    >>> open..for example..if i use cout to show the text's content..and ill
    >>> cannot to move my cursor to the content and edit...

    >
    >> Here is how to write a simple text editor with FLTK:

    >
    >> http://www.fltk.org/doc-1.3/editor.html

    >
    > That's more or less how *not* to write an editor. It starts at
    > the wrong end, and then gets lost in details before having
    > decided the generalities. (To be fair to the page: it looks to
    > me as though the "editor" was more of an excuse. That the
    > tutorial was about how to use FLTK, not how to write an editor
    > from scratch.)


    To write an editor the OP does not necessarily need to design it.
    The example in the tutorial is perfectly valid from this point of you view.
    Luca Risolia, Apr 1, 2013
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. San Diego Guy
    Replies:
    0
    Views:
    530
    San Diego Guy
    Aug 7, 2003
  2. Schultz
    Replies:
    3
    Views:
    541
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Feb 14, 2005
  3. =?Utf-8?B?a2Vu?=
    Replies:
    1
    Views:
    10,308
    Wiktor Zychla [C# MVP]
    Jan 23, 2006
  4. Sharon McCarty
    Replies:
    0
    Views:
    239
    Sharon McCarty
    Nov 24, 2004
  5. Frank Kurka
    Replies:
    8
    Views:
    287
Loading...

Share This Page