Ok so I was just wondering how console applications like top(1) for linux overwrite multiple lines. I want to know how to do it for C++ or C. I know how to overwrite 1 line by just putting a \r but I wanted to know how to do so for multiple lines, again like in top or if its even possible.
They use terminal escape sequences to move the cursor around the screen. A few even use direct framebuffer access, to treat the screen as an array of characters which can be directly indexed.
The curses (or ncurses) library is a good abstraction layer.
This may not directly address the question, but this sort of thing is dependent on the terminal and is commonly done with a curses implementation (ncurses is the most widely used).
Related
I'm already familiar with system ("cls") but it deletes all of the text above it and I just need to delete some of the text not all.
[Based on the reference to cls, I'm assuming this is code running under Windows.]
It depends on whether you need portability.
If you want (reasonably) portable code, you can use curses, an old text-mode Windowing library, originally written for terminals under Unix, but now implemented on most other systems (Linux, Windows, MacOS, etc.)
If you don't care about portability, Windows provides FillConsoleOutputCharacter, which will let you fill parts of a console with arbitrary characters. To "delete" text, you normally fill that area with spaces.
I have been using the very very old Turbo C++ 3.0 compiler.
During the usage of this compiler, I have become used to functions like getch(), getche() and most importantly clrscr().
Now I have started using Visual C++ 2010 Express. This is causing a lot of problems, as most of these functions (I found this out now) are non-standard and are not available in Visual C++.
What am I to do now?
Always try to avoid them if possible or try their alternatives :
for getch() --- cin.get()
clrscr -- system("cls") // try avoiding the system commands. check : [System][1]
And for any others you can search for them .
The real question is what you are trying to do, globally.
getch and clrscr have never been portable. If you're trying
to create masks or menus in a console window, you should look
into curses or ncurses: these offer a portable solution for
such things. If it's just paging, you can probably get away
with simple outputing a large number of '\n' (for clrscr),
and std::cin.get() for getch. (But beware that this will only
return once the user has entered a new line, and will only read
one character of the line, leaving the rest in the buffer. It
is definitely not a direct replacement for getch. In fact,
std::getline or std::cin::ignore might be better choices.)
Edit:
Adding some more possiblities:
First, as Joachim Pileborg suggested in his comment, if
portability is an issue, there may be platform specific
functions for much of what you are trying to do. If all you're
concerned about is Windows (and it probably is, since system(
"cls" ) and getch() don't work elsewhere), then his comment
may be a sufficient answer.
Second, for many consoles (including xterm and the a console
window under Windows), the escape sequence "\x1b""2J" should
clear the screen. (Note that you have to enter it as two
separate string literals, since otherwise, it would be
interpreted as two characters, the first with the impossible hex
value of 0x1b2.) Don't forget about possible issues of
redirection and flushing, however.
Finally, if you're doing anything non-trivial, you should look
into curses (or ncurses, they're the same thing, but with
different implementations). It's a bit more effort to put into
action (you need explicit initialization, etc.), but it has
a getch function which does exactly what you want, and it also
has functions for explicitly positionning the curser, etc. which
may also make your code simpler. (The original curses was
developed to support the original vi editor, at UCB. Any
editor like task not being developed in its own window would
benefit enormously from it.)
Well,
People, i have found the one best solution that can be used everywhere.
I simply googled the definitions of clrscr() and gotoxy() and created a header file and added these definitions to it. Thus, i can include this file and do everything that i was doing prior.
But, i have a query too.
windows.h is there in the definition. suppose i compile the file and make a exe file. Then will i be able to run it on a linux machine?
According to me the answer has to be yes. But please tell me if i am wrong and also tell me why i am wrong.
I have the following file. I wrote a C++ program that copies and pastes it on another one (along with additional stuff). However, I want to get rid of it. That is, I do not want to distribute both an executable and this file. I tried hard-coding its contents, but it is tedious since there are special characters (like ", \n, etc.), and a string variable may not always have the necessary memory to hold all of that data.
What else can I do?
The first thing that I was going to suggest is to encode the content of the file. Automatically, of course. I could imagine a nightmare of typing that in. That being said, there is a variety of tools out there. For example, bin2h. Or, for instance, there is a Qt Resources framework that is indeed nice, but is probably not worth pulling in a dependency unless you already use Qt.
However, if the content of whatever you are trying to cope with is large enough that it does not fit into memory, you have no other way but stick with having that file distributed externally along with a program. In fact, this is a pretty common way of doing things. For example, most of the heavy-weight (and not only) applications for OS X (and iOS) are distributed as "bundles", which is nothing but a zip compressed file with resource file(s) and executable(s). That as well might be a solution if you are targeting one of the platforms that promote such distribution practices.
Hope it helps :)
I am going to write a text editor in Qt which can provide highlighting/code completion/syntax analyzing for a programming language (toy language, for learning purpose).
At first, I thought of writing handcraft C++, which would be more comfortable for me since I'm more familiar. However, upon searching, I found out that flex/bison can simplify parser creation. Upon trying a few simple example, it seems the working examples accept input from standard input in terminal. So, I just want to know, can flex/bison accept input from the text editor widget in GUI framework (such as Qt, which I'm going to learn at the same time after I finish a few features in the parser engine), then later output the result back to the text editor?
If you do not want to use a FILE * pointer, you can also scan from in-memory buffers such as character arrays and nul-terminated C type strings by creating FLEX input buffers - yy_scan_string() creates a buffer from a null terminated string, yy_scan_bytes creates a buffer from a fixed length character array. See Multiple Input Buffers in the flex documentation for more information.
And if that does not meet your needs, you can also re-define the YY_INPUT macro for complete control - see Generated Scanner.
flex reads its input from yyin. If you point it to something that is not stdin... See here for example.
Edit: btw, yyin is a FILE *. You're using C++, which means you'd want to pass a stream instead. Please, read flex's documentation on C++ interfacing
Edit2: for the output... you're the one programming yacc/bison actions for the rules, and also the error handler. In that sense, you're given quite some freedom on what to do there. For example, you can "emit" highlighted code and also use the error handlers to point errors when analyzing the code. The completion would force you to implement at least part of the semantics (symbol table, etc), but that's a different story...
I'm having trouble working this out. Basically my program has a standard input, I want to modify this so if the user enters a space it automatically replaces it with a / whilst they're typing. I have no idea how i'd even go about doing this or if it is even possible so I am grateful for any help either way.
I believe that if you want to do manipulation of the text on the console like that, you're going to need a library such as ncurses.
You would need to turn off echoing of the characters typed and then echo them yourself after validating them, replacing characters as necessary. There are different ways to do that for different operating systems (e.g. Linux vs. Windows), and even more unfortunately, that can mean taking control of the entire screen just for this simple function since the backspace/delete key would need to be handled properly (even over multiple lines if necessary). In short, you're taking control away from the user while making yourself responsible for the common functionality that a user would expect, which makes things rather difficult because you can't know how certain terminals behave, not to mention issues with locales. It's easier just to replace the characters yourself after the input is received or issue an error message, depending on your purpose and specific use-case.
Edit: PDCurses is cross-platform, so you might try that if you're still interested. It's easier than coding for specific platforms.