Does this command still work if the output is redirected ?
thanks
Does this command still work if the output is redirected ?
thanks
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.
on what?
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.
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
in program abc.exe, then when I enterCode:system("cls");
abc > y.txt
I get a file, y.txt, that has one character: 0x0c
Also, if I have
in program def.exe, then when I enterCode:system("cls > z.txt);
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
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
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
cls.c:(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")).
qt.c:Code:#include <stdio.h> int main(void) { puts("Hello I'm in cls"); return 0; }
result:Code:#include <stdio.h> #include <stdlib.h> int main(void) { puts("Hello I'm in the first program"); system("cls"); return 0; }
See what happens when you make assumptions?Hello I'm in the first program
Hello I'm in cls
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:
cls.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; }
Regards,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; }
Dave
Regards,
Dave
Last edited by Dave Evans; 08-05-2004 at 08:44 PM.