hi,
I'm trying to write a text editor by using Files, Strings and Classes.
I'm not going to ask you to do it for me, I'm just a little confused. I don't know from where I should start and what should I do!
please help, thanks
Printable View
hi,
I'm trying to write a text editor by using Files, Strings and Classes.
I'm not going to ask you to do it for me, I'm just a little confused. I don't know from where I should start and what should I do!
please help, thanks
What do you already know?
I am assuming it's going to be a graphical text editor, so you will probably want to learn a GUI toolkit like Qt or WxWidgets.
I have never write a graphical program before. I'm so new in C++. not important it would be graphical or not, though graphical is better.
P.S. I'm working with visual studio 9
It's going to be quite complicated graphical or not. So how much C++ do you know?
I don't know how to explain!
I know about strings (a little), classes, files. but I've never work with them.
A line by line text editor can be done with rudimentary knowledge of all the things you mention... I recommend deciding what features that you want your text editor to have. What files will the editor be able to modify and how will you modify them?
I'm no master at C++ programming (yet), but as long as you know the basics (ie. Functions, Strings, Arrays etc), you might want to google for "Win32 Tutorials". There's a good one I'd highly recommend called "theForger's Win32 API Tutorial". I'm not sure if you can post links here, so I won't ;).
Cheers.
You're certainly allowed to post links here (related, appropriate links).
Try searching the Web.Quote:
Originally Posted by behzad_shabani
Start with something simple like ed
As salem says, start simple.
I wrote a simple editor (be aware that I sort of know what I'm doing - at least I think so :) ) in a couple of hours a little while back - it's actually posted somewhere in the C section. However, that editor is MUCH simpler than what "ed" can do.
The display part of an editor is not a particularly large part, if we look at a MODERN, big editor. However, if you haven't written anything fairly complex (like several thousands of lines of code) several times before, going the GUI route may end up more than doubling the task at hand (even more so if you are not writing a relatively complete editor).
--
Mats
thanks for all ur helps
I will read these tutorials and my C++ book. I hope that I can do it ;)
Write your own classes then ;)Quote:
Originally Posted by behzad_shabani
Alternatively, choose some C++ GUI library, e.g., FLTK, FOX, Gtkmm, Qt and wxWidgets, to name a few.
Is it possible to write this text editor in console environment?
like something I see in linux terminal?!!!!!!!!!
Of course you can. It's not as though there aren't a bunch of text editors that work in console environments.
If you want something like "normal" editing (moving around, replacing characters, etc.) you may want to look at the curses library. Our edlin challenge that mats referred to earlier didn't have such, but of course that's rather limiting.
Something like nano or vim or emacs? They are not any simpler (and probably more complex than average) GUI editors.
Like I told you, yeah it is. Have you ever used something like vi before? You could make a stripped down version of that with files and strings basically (though it might end up closer to an ed clone...). Anyway, you need to decide how you want to edit the files.
I sound like a broken record, maybe, but vi has interactive modes and you can do things like text substitution, delete, and insert text line by line and across the whole file. You need to work out the input that the user should provide to make these things happen, and how to process the results before you begin writing anything in C code. vi, for example, has a lot of obscure commands.
If you want to write a console text editor you might wind up using something like ncurses for output, so the user can read the file.
Any inkling that this is nontrivial yet? Congratulations on starting your first project, you'll be learning a lot. Get working on the specification.
The first steps are to make sure you have the right data structures to support all the editing operations you need to perform.
char buff[10000]; may seem easy enough, but consider the amount of memory you need to move around when you say delete a line, or do a global search/replace.
Whether you end up using
gotoxy() from conio.h
moveto() from ncurses.h, or
setTextPosition() from some GUI toolkit should (if you've done the design properly) have no impact on your core editor.
Even getting as far as say being able to do
myEdit.loadFile( "test.txt" );
myEdit.insertLine( 3, "hello world" );
myEdit.saveFile( );
would in itself teach you a lot about text management and such like, which would be good stuff for your subsequent attempts.
thanks all for help and giving me hope, specially to whiteflags and Salem.
I've found several Tutorials about win32 API. but more of them was using some new and complex codes, I hope that with your helps I will able to do my first C++ project.
If download and install Qt, there is a simple text editor in the examples in the documentation:
go here to download Qt:
http://trolltech.com/downloads/opensource/appdev
go here for the example:
http://doc.trolltech.com/4.4/mainwin...plication.html
Cheers!
There is a lot of information out there on using Qt:
Start with the Qt Documentation:
http://doc.trolltech.com/4.4/index.html
Then once you feel you understand the basics there is a great book available on Amazon:
http://www.amazon.com/Programming-Pr...0990251&sr=8-1
Start small and then work big!
Cheers!
In this link, in MainWindow class, there is private slots:. What's this?
See, That's the reason that I don't like QT very much.
Source coude written for the Qt library is'nt real C++ code.
It gets preprocessed by some compiler that generates the actual c++ code.
'slots' is one of the additional keywords that this precompiler understands.
Kurt
I thought you wanted to try to write your own, rather than just configure someone else's "editor" template.
yes, actually this is my C++ project in university.
In university we are learning Deitel C++ book. this text editor should written by C++ classes, strings and File.
we never practice graphical app, but my teacher said it'd better to work graphical!
once I ask him to help me a little, and he said you should write a string class....
I have one question from catch22.net tutorial
when I download its codes and compile it, it run successfully; but when I wrote the whole code by my own visual studio give C2440: '=' : cannot convert from 'HANDLE' to 'HINSTANCE' and error C2664: 'LoadImageA' : cannot convert parameter 1 from 'HANDLE' to 'HINSTANCE' errors. I don't know where's the problem, even when I completely copy and paste code this errors happens again :(
seems that you no longer support me through this.
If you don't wanna help any more, please close this thread
All we know was that you had a HANDLE parameter when you should have had an HINSTANCE. How are we supposed to know any more than that?
It is probably a case of "newer compilers being more finicky" - HINSTANCE is used for the first parameter to LoadImage(). It also seems like you are trying to assign a HINSTANCE to a HANDLE, which the compiler also won't like.
HINSTANCE is a form of HANDLE, but all handles aren't the same - so mixing and matching won't work. Use the correct type.
--
Mats
> seems that you no longer support me through this.
Who said that?
I though you'd gotten enough information to at least make a start, and be getting on with making some progress, maybe making a few mistakes (and learning stuff in the process), and generally having a fun time creating a real program.
If you've got new questions about what you've achieved, then ask them.
But we're not going to hand you a completed editor just because you ask enough questions.
I was :D, sorry about that :(
Once I wrote a simple editor by using MFC, but it was so simple and I think my teacher wouldn't accept that, so I decide to write a console editor.
I know that I have to write a string class.
but what a string needed?
how would it work?
I never mean that, I just need some simple help
What you need in your string class will depend on how you go about doing the rest of the editor, so it is hard to suggest anything. I can almost certainly say that you need a way to insert and remove character(s) at a certain index - but beyond that it's hard to say since it's a bit of a "where do we split between string class and generic text management".
--
Mats
try looking at the devC++ example if you have no idea how a graphical text editor looks like , or how you can make one!
personally id prefer to go with the console app rather than graphical editor , because thats a big change ! you know , you havent mastered C++ yet , still you are planning to write a graphical one ?
trust me pal, go for the easiest one if your teacher doesnt care ! (after that you ve mastered C++ abit more , you can switch to GUI version)
good luck
If you've discounted using any kind of GUI text widget to do a lot of the work for you, then it's back to basics.
Think about
- insert a character in a line
- delete a character
- insert a line
- delete a line
- cursor to next line
- find a string
-- from the current position
-- etc etc
-- the whole file
- replace a string
-- all the modes which find can do.
Sooner or later, you'll want to replace the array with something which is both dynamic and more efficient.Code:class editor {
private:
char buff[1000][100]; // easy to manipulate, but very slow
public:
insert( char c, int line );
}
But that's just fine - the whole C++ class thing allows you to alter the private data without affecting users.
Why do you think it's 2D?
It's 2D because that's what text looks like on a page.
would you tell me more about buffer and how it works, or give me an article.
I couldn't find anything :(
Like
buff[lineNumber][cursorXPosition];
It's just an array, there's no magic here.
But you'll find yourself moving a lot of data around when you insert/delete things.
But one step at a time OK, since it seems you're still in baby steps.
I know :D
It was a simple mistake, I understand it :D
sorry about that
I wrote this
but i can't handle the entered strings.
how could i control it that if user pressed enter, it goes to next line?Code:#define LINE 60
#define COL 100
class Editor
{
private:
char buff[LINE][COL];
public:
friend istream& operator >> (istream&, Editor&);
friend ostream& operator << (ostream&, const Editor&);
};
istream& operator >> (istream& input, Editor& str)
{
for (int i = 0; i < LINE; i++)
for (int j = 0; j < COL; j++)
input >> str.buff[i][j];
return input;
}
ostream& operator << (ostream& output, const Editor& str)
{
for (int i = 0; i < LINE; i++)
for (int j = 0; j < COL; j++)
output << str.buff[i][j];
return output;
}
Look at the FAQ about "How do I input data without user hitting enter" - which despite the title is actually close to "doing to the right thing when the user hits enter" too.
--
Mats
I couldn't find any thing, it returns no result
---------------------------
I found it
but it doesn't help!
I need something that return the character that user is entered right the moment so I check it and if it was '\n' string will go to another line
-------------------------------------
hope you understand what I mean :(
The FAQ details how to get the character that the user entered.
To get to the next line, presumably you would add one to the line number? (And potentially set the position to 0 as well.)
I would also say that having a operator<< or operator>> on the "Editor" is probably not the right thing - you probably should decouple the basic editor and the CONTENT that is being edited - sure, the editor needs to have some sort of handle to get hold of the content, but you do not want to write the editor to the file, you want to write the content to a file.
Better to get your object design roughly right to begin with than to find out later that it doesn't work
[A good editor can edit multiple files, but I don't think you'd want two differnet Editor objects to do that - just two instances of content].
--
Mats
Is it good idea to use link list and stack or somewhat to reading lines?
I see it in a C console based text editor.
You need either a list or a vector. A double-linked list is quite practical, as you often need to find the line "up" from where you are, as well as being able to walk "down" the list.
A vector (or fixed array, as Salem suggests) is somewhat easier to implement, but has a drawback on large files when inserting new lines (because every line after the new line will have to be moved).
As I suggested earlier, you can also have a hybrid approach of "blocks of lines" in a linked list - so for example a block may be a maximum of 16 lines.
The best way is that you "hide" how the lines are actually stored inside a class - that way, you can change the actuals of the implementation as and when you have a "better idea", without having to change the entire rest of the code.
--
Mats
I have a little knowledge of C++,
I have never worked with vectors and I don't know what it is!
I get it ;)
so I think I would work harder and learning new thing.
I wrote a simple multi-window (resizzeable) editor ages ago, but it was in C and ran on a console.
You could input text and cut an paste between windows and all that malarky also keyboard macros, open/save file, text search/replace, capture output of (unix) command and varius other useful features. Also display only windows for messages etc...
I didn't have any classes as such (whatever they are lol).
I still have the code, but only on a print out not on disk otherwise I would post it.
It's about 2000 lines.
It can't be much more effort making it a 'graphical' editor it just need a different display interface (I think).
Maybe I will type it in one day, 2000 lines, just like being back at school.
"I must do my homework"
"I must do my homework"
"I must do my homework"
I think I propable incorperated a lot of 'object oriented' concepts doing it.
I am still not sure what the term means - lol, but each window was essentially a structure. So when I wanted a new one I just malloced one and freed it when I destroyed the window. I could also save all the windows and reopen them all on restart
and 'tile' etc. (pretty easy to do). Never finished doing regular expression on it though, I was sackedby the - lol :D
I might have a copy on disk somewhere butI don't think I have working 'flopy drive' to read my old floppys.
std::string line;
cin.getline(line);
OK I used curses when I did mine, I asume it was the same/similar.
Anyway I dug out the old code and typed it in (hope there are no typing errors!)
CWIN is a pointer to a window I believe.
I noticed I used a function 'fix' maybe it was to fix the problem you are encountering?
I am guessing really. But none of the other curses routines needed it.
Actually as I used mutliple windows CWIN is actually one of an array
or window pointers, it's probably just a window pointer in yours?
I also had to maintain an array of the data in each window (bd[wn]) wn=window number.
It's also posible you need to set the value in the following functions.
leaveok(stdscr,TRUE);
scrollok(stdscr,FALSE);
However I do not know what your approach to it was so it might be nothing like mine.
Might be easier if you just look at how I used the cursors functions, rather than the code
to maintain my arrays of data.
Dunno if this will be of any help or not.
Note the scroll_win was a function I wrote to take care of scrolling you can ignore it for now, it onlyCode:struct wid **bd // array of structures of window data, position of window, of cursor row, column etc.... and data in each window
#defne OCOL bd[wn]->offcol // an offset for the column, used when some coulmns have 'scrolled' off the screen.
#defne WROW bd[wn]->rowpos //screen row
#defne WCOL bd[wn]->colpos //screen column
#define CWIN winarray[wn] //current window shorthand
WINDOW **winarray //array of window pointers
cur_up(){
int x,y;
getxy(CWIN,x,y);
if (y<1){ y=1;
OROW--;
scroll_win(NO_SCROLL);
x=1;
else { WROW--1;}
wmove(CWIN,x,y);
fix();
}
fix(){
int x,y;
getxy((CWIN,x,y);
wmove(CWIN,x,y);
wrefresh(CWIN);
WROW=x;
WCOL=y;
}
is needed when you reach the top of the window, which is not the case yet I believe.
Actually looking back at your posts I think you are going the wrong way about it, well if it is going to work anything like mine. Basically I turned all the terminal functions off and did them my self. ('raw more').
So nothing is echoed back to the screen, you just read each key pressed and process it as appropiate so you can't use getline or whatever.Code:initscr();
cbreak();
noecho();
nonl();
Well maybe you can but I think you will run into problems, I think you will end up with you wanting to do one thing and the terminal trying to do another.
Maybe they is the problem with the cursor up??
Basically I read a keystroke and put the character on the screen (and into my array of data) as appropiate, if it is the up arrow I move
the cursor up and move up a line in my array of data.
CArridge return for example moves the cursor down a line and to the start of the next line, and I updata my data indexes as appropiate.
It is more work (but not that much) but you end up with a much better editor. I mean getline() is just repeated calls to get character/keystroke.
I recommmed you try it my way or you will end up with a crap editor (like this one!!).
You see I ended up with multiple resizeable and moveable windows.
Plus I could save my array of structures on exit and reload it on start and every thing is back as you left it, right down to window size,
position and cursor positions. The only thing it could not do was write the code for you :D
Anyway good luck.
> I'm using pdcurses to handle the cursor, but how could I move cursor to previous line?
What classes do you have in your program so far?
Look at post #40 - that's an editor class.
Input and display are completely separate problems.
For example, this could be a main() for your tests
When you have an API for your editor which works, then you can choose what kind of user interface to wrap around it. But you'll have the foundation of an editor which can load and save things, and do things to the file when it's been loaded.Code:int main ( ) {
myEditor e;
e.new();
e.appendLine("Line 1");
e.appendLine("Line 3");
e.insertLine(2,"Line 2");
// and so on with delete, find, search, replace
e.save("test.txt");
}
Embedding lots of pdcurses stuff inside your editor class is NOT the way to go.
As curses works on Unix, Linux, Msdos, and Apple Mac it does not seem to be that bad a way to go to me, anyway it seems to me he has chosen a 'API' for his editor, and it is curses!