By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,365 Members | 3,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,365 IT Pros & Developers. It's quick & easy.

different "system()" for different OS

P: n/a
I want

system ("CLS")

if the system is WINDOWS

system ("CLEAR")

is the OS is a UNIX like.

How I have to do?

Nov 14 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"/* frank */" <__*******@despammed.com> wrote in message
news:2k************@uni-berlin.de...
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
Nov 14 '05 #2

P: n/a
/* frank */ <__*******@despammed.com> wrote:
I want 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.

--
Alex Monjushko (mo*******@hotmail.com)
Nov 14 '05 #3

P: n/a

"/* frank */" <__*******@despammed.com> a écrit dans le message de
news:2k************@uni-berlin.de...
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
Nov 14 '05 #4

P: n/a
"jacob navia" <ja***@jacob.remcomp.fr> writes:
[...]
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>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5

P: n/a

"Keith Thompson" <ks***@mib.org> a écrit dans le message de
news:ln************@nuthaus.mib.org...
"jacob navia" <ja***@jacob.remcomp.fr> writes:
[...]
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>


I do not want to clear any screen. You are answering to the original poster
isn't it?
Nov 14 '05 #6

P: n/a
In <cb**********@news-reader5.wanadoo.fr> "jacob navia" <ja***@jacob.remcomp.fr> writes:

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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7

P: n/a
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
<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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8

P: n/a
In <2k************@uni-berlin.de> /* frank */ <__*******@despammed.com> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9

P: n/a
"jacob navia" <ja***@jacob.remcomp.fr> writes:
[...]
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.