Simple text editor help
I have to do a simple text editor using the c language. But I don't know how to get started. The program should begin with a blank screen, waiting for the user to write something.The last line will be the "situation" line which explains what the program is doing or asking us to do. The user is also able to move the cursor using the arrow keys and the user is also able to load text files to the program from his hard drive and also make a text file of something the user has written in the program. He can load something by pressing CTRL-O or he can press CTRL-S to save something followed by the name of the text file. Our professor said that we have to use the library screenUtils to make the cursor move. How should I start? I'm trying to make this work but nothing really works.
This is an example of how the screen should look like:
First, google up your library, (which I just tried and could not locate in a quick search), then if needed, you miight want to start here, with the basics of console windows, if your OS is Windows:
Win32 Console Applications 1
I'm not sure how to check for Ctrl+O. If you don't find anything in a search of the forum, I'd jump onto Google. That's really an OS thing, rather than a part of standard C.
And of course, your library may cover a lot of this, so you don't have to.
So I found out what the screenUtils library does. Unfortunately it doesn't do anything with the CTRL+O commands. It has 3 functions:
void gotoxy(int x, int y); To move the cursor to a specific place on the screen.
void clrscr(void); To clear the screen and place the cursor on position (0,0).
void setColor(color text, color back); To change the color of the letters and the background.
What is your operating system?
Cursor moving is platform specific. Sane libraries that I know and you could use Windows' system calls or what ever they are, conio.h, or curses, which is the most portable from these.
^O is ASCII 0x0f, ^S is ASCII 0x13. For ^foo where foo belongs to [A-Z], the ASCII value is (foo - 'A' + 1).
His assignment states that he has to use the other library, so using conio.h is not OK for him.
Back to the Win32 console tutorial (it's a very good one), and get started with creating your console window as per the picture you posted.
Last time I did this (many DOS days ago), I used a large single dimension char array, which extended beyond the size of the text window, quite a bit. That allowed text to scroll and look natural.
I'd guessed that a 2 dimension char array with rows and columns would work better, but it was actually more of a pain to mess with, than a 1 dimension char array.
@fronty, is ^ the symbol for the Ctrl key or the Alt key or ?
Sorry, I didn't read the original post too well. But if he has to use that library, I guess there won't be much need for Win32 console library.
I wouldn't think about how I would display things. I would start by designing internal data structures used to hold the file, because it will be hell easier to write UI code which displays the menu stuff and the file contents from internal data structures, than first writing the UI and then trying adapt the internal datastructures to the UI code.
And ^ means control.
The link I gave him isn't for a console library - it's shows how to use the Windows console, without a library. In his case, a very limited library.
With Win32 console library I referred to the system library of Windows which offers functions to control a console.
Holy crap... I hope this isn't due on Monday...
Originally Posted by Fink
I have to give it in two weeks which I think is a lot of time to make this work. How should I ask the user to put a character? Should I do it with scanf? I'm sorry but I'm not the brightest c programmer. I'm thinking on putting the scanf (if needed) in a loop and if the user puts an arrow key I will use the gotoxy(int x, int y) function. But how will I make the thing with the last line work?
Sorry for my bad english.
Yes, you're going to have to work this character by character in a loop.
Originally Posted by Fink
You will need a rather large array of strings (a buffer) to hold the text and a fair bit of logic to position the cursor and text on the screen in some useable manner. Probably the simplest approach would be an array of about 10,000 strings at least 80 characters long.
Your code is going to have to do secondary tasks such as word wrapping and unwrapping, line breaks and paragraph breaks, dealing with backspacing, deletions, insertions, screen scrolling, etc. All done on a character by character basis, in real time, while the operator is typing.
It's been a long time since I did one of these and I'd suggest you get busy, 'cause you ain't gonna do it in an afternoon.
You do NOT have any time to spare. These kinds of assignments always take way longer than you think they will.
You can't use scanf() - all that normal keyboard input functions used for C, is buffered, (meaning you have to hit enter for it to be entered). You need to check out how windows can provide unbuffered input. Also in the MS library. Indeed, you should bookmark it for frequent reference.
Originally Posted by Adak
This guy's Prof. reminds me of one of my Bosses from years ago... This guy was a horrendous money waster and a flat out idiot. He would come to me and say "Can you do _______". I'd say "yes of course it can be done but it's going to take some time". So I'd get busy on the project, get it up to a prototype, something he could see... Then of course there'd be the software side of it. Well --and this guy never missed-- as soon as he saw the prototype he'd give it to the customer who, of course, immediately complained that it didn't work. My boss would bring it back and say "It doesn't work..." Every time I would try to explain that it needs the software before it's going to work... Without fail, he would toss it in the garbage and give the customer a refund, usually on the day I finished the software... In over 10 years and despite repeated explainations, nobody ever got him to understand that hardware and software work hand in hand. He probably scrapped about 50 perfectly good --patentable-- projects over the years.
Two weeks is no where near enough time to write, debug and deliver even a very basic text editor...