Writing a text editor

This is a discussion on Writing a text editor within the C++ Programming forums, part of the General Programming Boards category; I wrote this but i can't handle the entered strings. Code: #define LINE 60 #define COL 100 class Editor { ...

  1. #46
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    I wrote this
    but i can't handle the entered strings.

    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;
    }
    how could i control it that if user pressed enter, it goes to next line?
    Last edited by behzad_shabani; 01-19-2009 at 10:59 AM. Reason: coloring code

  2. #47
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    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. #48
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    I couldn't find any thing, it returns no result
    ---------------------------
    I found it
    Last edited by behzad_shabani; 01-19-2009 at 09:55 AM.

  4. #49
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    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
    Last edited by behzad_shabani; 01-19-2009 at 12:24 PM.

  5. #50
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.)

  6. #51
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    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.

  7. #52
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    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.

  8. #53
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    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.

  9. #54
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    I have a little knowledge of C++,
    I have never worked with vectors and I don't know what it is!

  10. #55
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by behzad_shabani View Post
    I have a little knowledge of C++,
    I have never worked with vectors and I don't know what it is!
    That tells me that you do not know quite enough to solve the task - yet at least.

    A vector is essentialy a dynamically sizing array - so you just add objects to it as you need, and in grows to accomodate the number of elements you have.

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

  11. #56
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    I get it
    so I think I would work harder and learning new thing.

  12. #57
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    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

    I might have a copy on disk somewhere butI don't think I have working 'flopy drive' to read my old floppys.

  13. #58
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    Quote Originally Posted by esbo View Post
    I wrote a simple multi-window (resizzeable) editor ages ago, but it was in C and ran on a console.
    .....
    thank you, I don't need C based text editor, I want to write a C++ class based text editor.

    I have a question. how could I get a line into a std::string variable?

  14. #59
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    std::string line;
    cin.getline(line);
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #60
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    Quote Originally Posted by Salem View Post
    std::string line;
    cin.getline(line);
    thanks (however I'm losing std::getline instead)
    another question, (I know I am pert ) I'm using pdcurses to handle the cursor, but how could I move cursor to previous line?
    when i press key up it bring the last line that i wrote.

Page 4 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 05-30-2007, 05:47 PM
  2. dtextp: a codeforming text editor
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-18-2007, 07:11 AM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Text editor with font color
    By KingoftheWorld in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-15-2003, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21