different "system()" for different OS

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • /* frank */

    #1

    different "system()" for different OS

    I want

    system ("CLS")

    if the system is WINDOWS

    system ("CLEAR")

    is the OS is a UNIX like.

    How I have to do?

  • Allan Bruce

    #2
    Re: different "system()& quot; for different OS


    "/* frank */" <__frank__@desp ammed.com> wrote in message
    news:2kdochF16e h7U1@uni-berlin.de...[color=blue]
    > I want
    >
    > system ("CLS")
    >
    > if the system is WINDOWS
    >
    > system ("CLEAR")
    >
    > is the OS is a UNIX like.
    >
    > How I have to do?
    >[/color]

    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


    Comment

    • Alex Monjushko

      #3
      Re: different &quot;system()& quot; for different OS

      /* frank */ <__frank__@desp ammed.com> wrote:[color=blue]
      > I want[/color]
      [color=blue]
      > system ("CLS")[/color]
      [color=blue]
      > if the system is WINDOWS[/color]
      [color=blue]
      > system ("CLEAR")[/color]
      [color=blue]
      > is the OS is a UNIX like.[/color]

      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 (monjushko@hotm ail.com)

      Comment

      • jacob navia

        #4
        Re: different &quot;system()& quot; for different OS


        "/* frank */" <__frank__@desp ammed.com> a écrit dans le message de
        news:2kdochF16e h7U1@uni-berlin.de...[color=blue]
        > I want
        >
        > system ("CLS")
        >
        > if the system is WINDOWS
        >
        > system ("CLEAR")
        >
        > is the OS is a UNIX like.
        >
        > How I have to do?
        >[/color]

        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


        Comment

        • Keith Thompson

          #5
          Re: different &quot;system()& quot; for different OS

          "jacob navia" <jacob@jacob.re mcomp.fr> writes:
          [...][color=blue]
          > 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[/color]

          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_Keit h) kst-u@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.

          Comment

          • jacob navia

            #6
            Re: different &quot;system()& quot; for different OS


            "Keith Thompson" <kst-u@mib.org> a écrit dans le message de
            news:lnhdstc1gq .fsf@nuthaus.mi b.org...[color=blue]
            > "jacob navia" <jacob@jacob.re mcomp.fr> writes:
            > [...][color=green]
            > > 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[/color]
            >
            > 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>
            >[/color]

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


            Comment

            • Dan Pop

              #7
              Re: different &quot;system()& quot; for different OS

              In <cbscn3$bv9$1@n ews-reader5.wanadoo .fr> "jacob navia" <jacob@jacob.re mcomp.fr> writes:

              [color=blue]
              >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++)[/color]
              ^^^^^^^^^
              Syntax error in C89.
              [color=blue]
              > printf("\n");
              >}
              >
              >Inefficient, granted, but it would work :-)[/color]

              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: Dan.Pop@ifh.de

              Comment

              • Dan Pop

                #8
                To CLS or not to CLS (was: different &quot;system()& quot; for different OS)

                In <lnhdstc1gq.fsf @nuthaus.mib.or g> Keith Thompson <kst-u@mib.org> writes:
                [color=blue]
                ><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
                >unnecessaril y erases information from *my* screen, I consider it a
                >hostile act.</OT>[/color]

                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: Dan.Pop@ifh.de

                Comment

                • Dan Pop

                  #9
                  Re: different &quot;system()& quot; for different OS

                  In <2kdochF16eh7U1 @uni-berlin.de> /* frank */ <__frank__@desp ammed.com> writes:
                  [color=blue]
                  >I want
                  >
                  >system ("CLS")
                  >
                  >if the system is WINDOWS
                  >
                  >system ("CLEAR")
                  >
                  >is the OS is a UNIX like.
                  >
                  >How I have to do?[/color]

                  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: Dan.Pop@ifh.de

                  Comment

                  • Keith Thompson

                    #10
                    Re: different &quot;system()& quot; for different OS

                    "jacob navia" <jacob@jacob.re mcomp.fr> writes:
                    [...][color=blue]
                    > I do not want to clear any screen. You are answering to the original poster
                    > isn't it?[/color]

                    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_Keit h) kst-u@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.

                    Comment

                    Working...