Applications can manipulate text based consoles or terminals, and change their colors, set cursor position. The supported approaches are:
For Unix-like systems: There is ANSI escape code.
For Windows systems: There is APIs like SetConsoleTextAttribute.
...
but, is there any lightweight and portable C/C++ library which handles differences between operating systems just for colors and cursor? and do nothing if it was technically impossible but best effort.
Note: I'm not searching for heavy external tools to emulate unix-like terminals (like Cygwin, Msys-rxvt, ...). I think a simple portability will be achieved with Windows APIs and ANSI escape codes. And not ncurses because it's heavy and has many functionality to full control console and I think it needs emulation.
Alright, i finally found a portable and easy to use library: rlutil.h
Usage:
#include <iostream>
#include "rlutil.h"
int main()
{
for (int i = 0; i < 16; i++)
{
rlutil::setColor(i);
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
but, i will be glad for other suggestions.
Related
I'm trying to print out the smiley face (from ascii) based on the amount of times the user asks for it, but on the console output screen, it only shows a square with another one inside of it. Where have I gone wrong?
#include <iostream>
using namespace std;
int main()
{
int smile;
cout << "How many smiley faces do you want to see? ";
cin >> smile;
for (int i = 0; i < smile; i++)
{
cout << static_cast<char>(1) << "\t";
}
cout << endl;
return 0;
}
ASCII does not have smileys (so in ASCII you'll have :-) and you expect your reader to understand that as a smiley). But Unicode has several ones, e.g. ☺ (white smiling face, U+263A); see http://unicodeemoticons.com/ or http://www.unicode.org/emoji/charts/emoji-list.html for a nice table of them.
In 2017, it is reasonable to use UTF8 everywhere (in terminals & outputs). UTF-8 is a very common encoding for Unicode, and many Unicode characters are encoded in several bytes in UTF-8.
So in a terminal using UTF8, with a font with many characters available, since ☺ is UTF8 encoded as "\342\230\272", use:
for (int i = 0; i < smile; i++)
{
cout << "\342\230\272" << "\t";
}
In 2017, most "console" are terminal emulators because real terminals -like the mythical VT100- are today in museums, and you can at least configure these terminal emulators to use UTF-8 encoding. On many operating systems (notably most Linux distributions and MacOSX), they are using UTF-8 by default.
If your C++11 compiler accepts UTF8 in strings (and a UTF8 source file), as most do today, you could even have "☺" in your source code. To type that you'll often use some copy and paste technique from an outside source. On my Linux system I often use some Character Map utility (e.g. run charmap in a terminal) to get them.
In ASCII, the character of code 1 is a control character, the Start Of Heading. Perhaps you are confusing ASCII with CP437 which is no more used (but in 1980s encoded a smiley-thing at code 1).
You need to use Unicode and understand it. Today, in 2017, you cannot afford using other encodings (they are historical legacy for museums) externally. Of course if you use weird characters, you should document that the user of your program should use some font having them (but most common fonts used in terminal emulators accept a very wide part of Unicode, so that is not a problem in practice). However, on my Linux computers, many fonts are lacking U+1F642 Slightly Smiling Face (e.g. "\360\267\231\202" in a C++ program) which appeared only in Unicode7.0 in 2014.
Just do this in Visual Studio Code:
for print;
cout<<"\2";
So I'm currently learning C++11 and I'm getting the hang of it. I want to play around with using a different language and since I'm Vietnamese, I want to make a C++ program that uses Vietnamese characters.
So how can I display Vietnamese characters the same way that English is displayed, which is like this:
cout << "Hello. This is English" << endl; //English
cout << "Chào. Đây là tiếng Việt." << endl; //Vietnamese
I heard that C++ has <locale>. Does it help make the Vietnamese characters appear?
You may be running into a problem with your environment. You don't say what platform/environment you are running in, but take the following program:
#include <iostream>
#include <cstdlib>
int main()
{
std::cout << u8"Chào thế giới!" << std::endl;
return EXIT_SUCCESS;
}
This yields the following output from iTerm on Mac OS X:
Chào thế giới!
With other (non-unicode) environments, using the same code, you may get UTF-8 characters interpreted as ASCII on output. I don't know what the Windows command line will yield, but if you are using an IDE, your IDE may or may not render UTF-8, independently of whether your shell does or doesn't.
Here's a web example.
https://code.sololearn.com/c39N9RN6b4Md/#cpp yields:
Chào thế giới!
But http://ideone.com/OkkUZs running exactly the same code yields:
Chào thế giới!
It's probably also worth pointing out that in C++ to properly process UTF-8 strings, count "characters", ensure your strings are valid UTF-8, etc. you will likely want to use a Unicode library--working with Unicode is non-trivial.
Personally, I have found both UTFCPP and TinyUTF8 to be excellent libraries - reasonably small, simple and effective.
Hope that helps.
#include <iostream>
#include <io.h>
#include <fcntl.h>
int main() {
_setmode(_fileno(stdout), _O_U16TEXT);
std::wcout << L"Chào. Đây là tiếng Việt.";
}
This is a solution that works for windows. Unfortunately it's not portable to other platforms.
When dealing with console input (stdin,std::cin) is there a portable way in C++ to manage the various actions that a user may perform like:
Backspace/Delete
List item
Left/Right arrow keys (moving cursor back/forth insert text)
For example in windows when using std::cin (eg: std::cin >> s;), it allows for arrow keys, however when using the same bit of code on linux, the arrow keys are assumed as part of the input, the cursor is not moved around.
I know of various TUI frameworks like curses and ncurses that provide such functionality however they are more than what is required.
I'm hoping there's a simple solution based on the standard libraries, or even a lightweight open source library that might have a std::getline like feature that is portable across the more popular OSes.
Things like backspace and delete are typically handled by the
system; when you read from a terminal, you only get the input
when the user presses enter.
What the system does is usually fairly limited. In particular,
I don't know of any that do things like file name completion.
If more than what the system does is desired, I would recommend
looking into the readline library, used by many GNU programs
(bash, gdb, etc.). It's available separately from the
applications which use it. (Two small warnings: I don't know
how good its support is for native Windows, and I'm not sure
which license it is under: GPL or LGPL.)
readline is a good choice for Linux, but it's GPL! I use the following code to compile on Windows and Linux:
#ifdef USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
...
void getline(char *buf)
{
#ifdef USE_READLINE
char *tmp;
tmp = readline(PROMPT);
if(strncmp(tmp, buf, MAXLENGTH)) add_history(tmp); // only add new content
strncpy(buf, tmp, MAXLENGTH);
buf[MAXLENGTH]='\0';
free(tmp);
#else
std::cout<<PROMPT;
std::cin.get(buf,MAXLENGTH);
std::cin.ignore(); // delete CR
#endif
}
I am making a Library Management System in Xcode using C++. As Xcode does not support libraries such as conio.h and system "cls" does not work in it. What code should I use to clear the screen when I want it to shift from one menu to the other?
Check this out.
https://discussions.apple.com/thread/1064635?start=0&tstart=0
There is no direct way to do that; the system() command will not work on Mac (Unix). One option is to add a lot of spaces using code i.e.\n or other way is to use curses library
#include < curses.h > (curses.h) and then use system("clear"), which basically will do the same thing. So, its better to print spaces manually using the code rather than using some library.
One more thing you can do for POSIX (Unix, Linux, Mac OSX, etc) based systems [Note: I have not tested it myself]:
#include < unistd.h >
#include < term.h >
void ClearScreen()
{
if (!cur_term)
{
int result;
setupterm( NULL, STDOUT_FILENO, &result );
if (result <= 0) return;
}
putp( tigetstr( "clear" ) );
}
You'll have to link to the proper library (one of -lcurses, -lterminfo, etc.) to compile that last one. (Source: http://www.cplusplus.com/forum/articles/10515/)
I want to pass a value of an input variable in my program lets say#1 to another program #2 and i want #2 to print the data it got to screen, both are needed to be written in c++. The this will be on Linux.
Depending on the platform there are a number of options available. What you are trying to do is typically called inter-process communication (IPC).
Some options include:
Sockets
Pipes
Queues
Shared Memory
What is easiest is probably dependent on the platform youa are using.
As always, there is a Boost library for that (God, I like Boost).
Nic has covered all the 4 that I wanted to mention (on the same machine):
Sockets
Pipes
Queues
Shared Memory
If writing system calls is troublesome for you, you may want to use the following libraries:
Boost http://www.boost.org/
Poco http://pocoproject.org/blog/
Nokia Qt http://qt.nokia.com/
Something you can read from Qt portable IPC: only QSharedMemory?
If effeciency is not prime concern then use normal file i/o.
else go for IPC to do so.
As far as Windows is concern you have following options :
Clipboard ,
COM ,
Data Copy ,
DDE ,
File Mapping ,
Mailslots ,
Pipes ,
RPC ,
Windows Sockets
For Linux , use can use Name Pipes(efficient) or sockets.
If you're on Windows, you can use Microsoft Message Queueing. This is an example of queue mentioned previously.
If the data to be passed is just a variable, then one of the option is to set it as Environment Variable [ Var1 ] by program #1 and access it, in Program #2 [ if both are running on same env/machine ]. Guess this will be the easiest one, instead of making it complex, by using IPC/socket etc.
I think most of the answers have address the common IPC mechanisms. I'd just like to add that I would probably go for sockets because it's fairly most standard across several platforms. I decided to go for that when I needed to implement IPC that worked both on Symbian Series 60 and Windows Mobile.
The paradigm is straightforward and apart from a few platform glitches, the model worked the same for both platforms. I would also suggest using Protocol Buffers to format the data you send through. Google uses this a lot in its infrastructure. http://code.google.com/p/protobuf/
DBUS
QtDbus
DBus-mm
In response to your comment to Roopesh Majeti's answer, here's a very simple example using environment variables:
First program:
// p1.cpp - set the variable
#include <cstdlib>
using namespace std;;
int main() {
_putenv( "MYVAR=foobar" );
system( "p2.exe" );
}
Second program:
// p2.cpp - read the variable
#include <cstdlib>
#include <iostream>
using namespace std;;
int main() {
char * p = getenv( "MYVAR" );
if ( p == 0 ) {
cout << "Not set" << endl;
}
else {
cout << "Value: " << p << endl;
}
}
Note:
there is no standard way of setting an environment variable
you will need to construct the name=value string from the variable contents
For a very dirt and completely nonprofessional solution you can do it like me.
Save the variable in to a file and then read it (in an infinite loop every x time) with the other program.
fsexample.open("F:/etc etc ...");
fsexample >> data1 >> data2; // etc etc
and on the other side
fsexample.open("F:/etc etc ...");
fsexample << data1 << data2; // etc etc
The trick is that F is a virtual drive created with ramdisk so it is fast
and heavy-duty proof.
You could have problem of simultaneous access but you can check it with
if (!fsexample.is_open()) {
fsexample_error = 1;
}
and retry on failure.