Saturday, January 28, 2017

Digest for comp.lang.c++@googlegroups.com - 21 updates in 6 topics

alexo <alessandro.volturno@libero.it>: Jan 28 02:55PM +0100

Hi all,
I'd like to write a console program for a text-based game.
(I know it's overdated, but I like the console - it reminds me when I
started using computers). And probably I will be the only one to use it :(
 
Sometimes when the buffer on screen is too clumsy, thers the need to
clear the screen. I know of the system() function defined in the
<stdlib.h> header file, and I know that in MS-Windows I get what I want
calling system("cls").
 
I also need the program run under linux, were the system function would
be system("reset"). So my question is: is there a way to know from the
inside of a C/C++ program which OS is running it?
 
I could write something like:
 
 
 
#ifdef SOME_WINDOWS_VARIABLE
#define CLEARSCR() system("cls")
 
#ifdef SOME_LINUX_VARIABLE
#define CLEARSCR() system("rset")
 
 
and at need call generically CLEARSCR();
 
Thank you for your replies,
Alex
Paavo Helde <myfirstname@osa.pri.ee>: Jan 28 04:19PM +0200

On 28.01.2017 15:55, alexo wrote:
> #define CLEARSCR() system("cls")
 
> #ifdef SOME_LINUX_VARIABLE
> #define CLEARSCR() system("rset")
 
Why macros? What is wrong with functions?
 
> and at need call generically CLEARSCR();
 
For console programs there are some portable libraries like ncurses. But
if you just want to clear the screen then this would probably be an
overkill.
 
In practice, for telling apart Windows and Linux you could study some
environment variables like OS, OSTYPE, WINDIR etc. But do you really
need to distinguish them? Just run both cls and reset and be done.
 
system("cls 2>/dev/null");
system("reset 2>NUL");
 
hth
Paavo
alexo <alessandro.volturno@libero.it>: Jan 28 04:55PM +0100

Il 28/01/2017 15:19, Paavo Helde ha scritto:
> system("reset 2>NUL");
 
> hth
> Paavo
 
 
Hi Paavo,
thank you for your quick reply.
 
Maybe I was not as clear as I hoped.
What I need to do is to compile my sourcecode under windows and then
compiling take the same sourcecode (without modifying a thing) and under
linux. I'm too new to programming to make a cross-compilation.
 
The macro serves to generically say to the program: CLEARSCR()
If I'm under Windows it should expand to: system("cls"),
if I'm under linux it should expand to system("reset").
 
At least this would be my intention.
 
I'm studying PDcurses - it is portable. As far as I know ncurses is tied
to linux only (but I may be wrong).
 
Anyway this is not what I want. It is not my intention to build a Text
User Interface; it's just OK the output cout or printf put to the console.
 
thank you
Paavo Helde <myfirstname@osa.pri.ee>: Jan 28 06:20PM +0200

On 28.01.2017 17:55, alexo wrote:
> What I need to do is to compile my sourcecode under windows and then
> compiling take the same sourcecode (without modifying a thing) and under
> linux. I'm too new to programming to make a cross-compilation.
 
OK, I see you want to find out this at compile time. My earlier
suggestion was to just issue both commands, with any errors suppressed,
and not bother even to find out which platform you are on, neither at
compile or run time. But I see you insist on doing things "properly",
that's OK too.
 
There is no standard C++ way to find out the OS at the compile time
(presumably because C++ does not even require an OS to be present), but
in practice there are some predefined preprocessor macros which you can
check, e.g.
 
#if (defined (_WIN32) || defined (_WIN64))
void ClearScreen() {
system("cls");
}
#elif (defined (LINUX) || defined (__linux__))
void ClearScreen() {
system("reset");
}
#else
#error Unsupported platform.

No comments: