different "system()" for different OS

F

/* frank */

I want

system ("CLS")

if the system is WINDOWS

system ("CLEAR")

is the OS is a UNIX like.

How I have to do?
 
A

Allan Bruce

/* frank */ said:
I want

system ("CLS")

if the system is WINDOWS

system ("CLEAR")

is the OS is a UNIX like.

How I have to do?

use the pre-processor e.g.

#ifdef _WINDOWS
system("cls");
#else
system("clear");
endif

I am not sure what to check for, if you want to do more checks than this,
but you get the idea
Allan
 
A

Alex Monjushko

/* frank */ said:
system ("CLS")
if the system is WINDOWS
system ("CLEAR")
is the OS is a UNIX like.

First of all, Unix is case-sensitive.

Second of all, the preprocessor is your friend.

void my_clear_screen(void)
{
#ifdef SYS_IS_WINDOWS
system("cls");
#elif SYS_IS_UNIX
system("clear");
#else
#error my_clear_screen not supported on this platform
#endif
}

Of course, it is up to you to define SYS_IS_WINDOWS or SYS_IS_UNIX.
 
J

jacob navia

/* frank */ said:
I want

system ("CLS")

if the system is WINDOWS

system ("CLEAR")

is the OS is a UNIX like.

How I have to do?

Most of the windows compilers I know support the

_WIN32

symbol that is automatically defined.

#ifdef _WIN32
.... windows specific code
#else
... other systems
#endif

If you do not like #ifdef'ed code,
in both systems this would clear the screen:

void cls(void)
{
for (int i = 0; i<500;i++)
printf("\n");
}

Inefficient, granted, but it would work :)

jacob
 
K

Keith Thompson

jacob navia said:
If you do not like #ifdef'ed code,
in both systems this would clear the screen:

void cls(void)
{
for (int i = 0; i<500;i++)
printf("\n");
}

Inefficient, granted, but it would work :)

jacob

It would also (on many systems) leave the cursor at the bottom of the
screen whereas "CLS" or "clear" typically leaves the cursor at the top
of the screen.

It also assumes that stdout points to a "screen" of some sort (which,
depending on the environment in which the program is run, may be a
perfectly reasonable assumption).

<OT>You should also consider why you want to clear the screen in the
first place. If a program other than a full-screen editor
unnecessarily erases information from *my* screen, I consider it a
hostile act.</OT>
 
J

jacob navia

Keith Thompson said:
It would also (on many systems) leave the cursor at the bottom of the
screen whereas "CLS" or "clear" typically leaves the cursor at the top
of the screen.

It also assumes that stdout points to a "screen" of some sort (which,
depending on the environment in which the program is run, may be a
perfectly reasonable assumption).

<OT>You should also consider why you want to clear the screen in the
first place. If a program other than a full-screen editor
unnecessarily erases information from *my* screen, I consider it a
hostile act.</OT>

I do not want to clear any screen. You are answering to the original poster
isn't it?
 
D

Dan Pop

If you do not like #ifdef'ed code,
in both systems this would clear the screen:

void cls(void)
{
for (int i = 0; i<500;i++)
^^^^^^^^^
Syntax error in C89.
printf("\n");
}

Inefficient, granted, but it would work :)

Would it? Try it on an xterm (Unix's most popular terminal emulator for
X11) in Tektronix mode...

Dan
 
D

Dan Pop

In said:
<OT>You should also consider why you want to clear the screen in the
first place. If a program other than a full-screen editor
unnecessarily erases information from *my* screen, I consider it a
hostile act.</OT>

It's not editors vs others programs, it's programs operating in full
screen mode vs programs operating in teletype mode.

ANY program operating in full screen mode (top and friends, advanced
pagers, visual editors, text mode web browsers, games, interfaces to
various services etc etc) is supposed to start by clearing the screen).

NO program operating in tty mode has any business to attempt clearing
the "screen". If it's operating in tty mode, it should assume that both
paper and glass teletypes can be used as output devices.

And if someone wants to write a full screen application, clearing the
screen is the last of his worries, as the API used for generating full
screen output will provide a primitive for this purpose.

Dan
 
D

Dan Pop

In said:
I want

system ("CLS")

if the system is WINDOWS

system ("CLEAR")

is the OS is a UNIX like.

How I have to do?

Identify reserved identifiers predefined by the preprocessors running on
the platforms of interest and test their existence. Untested example:

#ifdef _WIN32
#define COMMAND "CLS"
#elif defined __unix__
#define COMMAND "clear"
#else
#error "Platform not supported."
#endif

and, of course, use COMMAND as the argument of the system() call.

I hope this is only a generic example: no well designed program has
any business invoking the shell command that clears the screen.

Dan
 
K

Keith Thompson

jacob navia said:
I do not want to clear any screen. You are answering to the original poster
isn't it?

Of course. Actually, this being Usenet, I'm talking to anyone who
happens to read my article. (I could have made that clearer.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,144
Messages
2,570,823
Members
47,369
Latest member
FTMZ

Latest Threads

Top