How to edit a text file ?

C

Chrisshia

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...
 
C

Chrisshia

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.
 
S

Stefan Ram

Sam said:
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.
 
S

Stefan Ram

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.
 
J

James Kanze

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.)
 
J

James Kanze

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.
 
R

Rui Maciel

Sam said:
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
 
8

88888 Dihedral

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.
 
L

Luca Risolia

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top