Thread: just a question about the system("cls") command

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    46

    just a question about the system("cls") command

    Does this command still work if the output is redirected ?

    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    That depends....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    on what?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    On the system's implementation of the cls command. If it modifies video memory directly then file redirection doesn't matter. If it simply prints a bunch of newlines then the newlines will go to the file instead of the screen if the output is redirected.

    Not that those are the only 2 possible choices on ways the system could clear the screen, but that gives you 2 examples of ways it could work differently and give different results.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    On my system (Windows XP) "cls" emits a control-l character (0x0c) to stdout. So, if I enter

    cls >a.txt

    at the command prompt, I get a file, a.txt, with the single character, 0x0c.

    Using system("cls") in a program gives the same result as if I enter cls from a command line. I have Borland bcc32 and Microsoft Visual C++ compilers --- both give the same result.

    That means, if I have
    Code:
      system("cls");
    in program abc.exe, then when I enter

    abc > y.txt

    I get a file, y.txt, that has one character: 0x0c


    Also, if I have
    Code:
      system("cls > z.txt);
    in program def.exe, then when I enter

    def

    the result is a file, z.txt, with the single character 0x0c.

    In neither case is the screen cleared in the window from which I invoke the program.


    Your Mileage May Vary (so why not try it yourself).

    By the way, the answer to all questions is, "It depends."

    Dave

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Of course the OP didn't specify what the output was redirected to. If its redirected to another console then you'll get different results then if you were to redirect to a printer or a LCD display. Also since cls just another program it could also reformat your harddrive

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Of course, use of system("xxx") in a program is a security problem waiting to blossom, if the program xxx has been overwritten by some malicious intruder. (But this is also true if you invoke the program from the command line or by clicking an icon.)

    In this case, however, I believe that "cls" is not a program, but is a built-in command. (Try it: make a program that prints "Hello, world", rename the executable to cls.exe, then invoke it from a program that uses system("cls")).

    Anyhow, I agree with the previous statement that "It depends"; I just reported some results on my system and suggested an experiment if the original poster, or anyone else, happens to be interested.

    It's easier, and quicker, to do it than it is to talk about it.

    Dave

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    (Try it: make a program that prints "Hello, world", rename the executable to cls.exe, then invoke it from a program that uses system("cls")).
    cls.c:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      puts("Hello I'm in cls");
      return 0;
    }
    qt.c:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      puts("Hello I'm in the first program");
      system("cls");
      return 0;
    }
    result:
    Hello I'm in the first program
    Hello I'm in cls
    See what happens when you make assumptions?

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    The thing is that I didn't make any predictions. I reported what happened on my system, and invited any interested parties to try it on their own. You obviously don't need the practice, but I see lots of inquiries that could be answered by the posters if they just tried it. I would like to know what compilers and operating systems give different results. The system() command is part of the standard C language specification, but what the system does with the parameter of the system("foo") call is obviously system dependent.

    [edit]
    Actually, upon further reflection I see that I did assume that the original poster had a question about some Windows system, since his question was about the cls command. I also assumed he was genuinely curious, and not looking for something tricky. Come to think of it, I think that DRDOS had cls as an executable rather than a built-in.
    [/edit]

    I think it's exciting to learn new stuff, and I think lots of people just getting started are trying to get too much information by asking, "What would happen if ...", instead of just doing it. This discussion is valuable since it points out that some things depend on system implementation, and I hope that inquiring minds will get comfortable with trying things on their own.

    I repeated the experiment on Windows XP Home and Windows XP Pro, and got the same results: the screen was always cleared by system("cls") with no indication that my cls.exe was ever executed (Using Borland bcc32 5.5.1, but I doubt that it's compiler-dependent.)

    (Of course when I used system("cls.exe") it went through my version of cls.)

    qt.c:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      printf("Hello I'm in program %s.\n", argv[0]);
      printf("Press enter to execute system(\"cls\"): ");
      getchar();
      system("cls");
      printf("Returned from system(\"cls\").\n");
      printf("Press Enter to return from argv[0].exe: ");
      getchar();
      return 0;
    }
    cls.c:

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      printf("Hello I'm in %s.\n", argv[0]);
      printf("Press Enter to return to the calling program: ");
      getchar();
      return 0;
    }
    Regards,

    Dave




    Regards,

    Dave
    Last edited by Dave Evans; 08-05-2004 at 08:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on 'top' command
    By muthus in forum Linux Programming
    Replies: 3
    Last Post: 01-21-2008, 01:18 AM
  2. command line arguments question
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 10-17-2005, 03:57 AM
  3. exe files in -c- language
    By enjoy in forum C Programming
    Replies: 6
    Last Post: 05-18-2004, 04:36 PM
  4. command line param question
    By moi in forum C Programming
    Replies: 1
    Last Post: 08-05-2002, 08:07 PM
  5. question about those command thngy's
    By face_master in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2001, 08:54 AM