Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to clear the stdout ?

Reply
Thread Tools

how to clear the stdout ?

 
 
Joseph
Guest
Posts: n/a
 
      09-16-2004
Hi all,

I am doing my assignment and have a question ,how can I clear the standard
output(screen)?

following is the fake code:

=====================================

main(){


for loop(int i++){

printf (" %d",i);

}

}
====================================

the problem is,I want to clean the previous output number and then print
only the new number .So it is like a on-screen counter somehow.




Thanks a lot guys!!!
 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      09-16-2004
"Joseph" <(E-Mail Removed)> wrote in message
news:Xns9566AE53A1569wutongjoe@203.96.216.21...
> Hi all,
>
> I am doing my assignment and have a question ,how can I clear the standard
> output(screen)?
>
> following is the fake code:
>
> =====================================
>
> main(){
>
>
> for loop(int i++){
>
> printf (" %d",i);
>
> }
>
> }
> ====================================
>
> the problem is,I want to clean the previous output number and then print
> only the new number .So it is like a on-screen counter somehow.


This sounds pretty similar to a FAQ. See the FAQ
(http://www.parashift.com/c++-faq-lite/), section 15 ("Input/output via
<iostream> and <cstdio>"), question 20 ("How can I clear the screen? Is
there something like clrscr()?").

There are some (non-portable) ways to do what you want. For instance, on
Windows, using VS.NET2003, the following code displays a counter that is
periodically overwritten:

#include <stdio.h>

int main() {
int i = 10;
printf("Counter: %d", i);
for ( int i = 11; i < 20; ++i ) {
// Use a "hack" that uses backspace to erase the number
printf( "\b\b" );
printf( "%d", i );

// Do something to pause here, like Sleep()
for (int j = 0; j < 100000000; ++j ){}
}
printf("\n");
return 0;
}

--
David Hilsee


 
Reply With Quote
 
 
 
 
Gernot Frisch
Guest
Posts: n/a
 
      09-16-2004
The most portable way is:
system("cls");

HTH,
Gernot


 
Reply With Quote
 
Materialised
Guest
Posts: n/a
 
      09-16-2004
Gernot Frisch wrote:
> The most portable way is:
> system("cls");
>
> HTH,
> Gernot
>
>

bash: cls: command not found
--
perl -e 'printf "%silto%c%sal%c%s%ccodegurus%corg%c", "ma", 58, "mw",
107, 'er', 64, 46, 10;'
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      09-16-2004
Joseph wrote:
> ...
> I am doing my assignment and have a question ,how can I clear the

standard
> output(screen)?
>
> following is the fake code:
>
> =====================================
>
> main(){
>
>
> for loop(int i++){
>
> printf (" %d",i);
>
> }
>
> }
> ====================================
>
> the problem is,I want to clean the previous output number and then print
> only the new number .So it is like a on-screen counter somehow.
> ...


Instead of clearing the entire screen you might want to try to print
every new number at the same place where the previous one was printed
(thus overwriting the previous one) by doing it like this

printf("%d\r", i);

I don't think it is 100% portable in theory, but in practice this will
work on many platforms.

--
Best regards,
Andrey Tarasevich

 
Reply With Quote
 
Nicolas Pavlidis
Guest
Posts: n/a
 
      09-16-2004
Joseph <(E-Mail Removed)> writes:

> Hi all,
>
> I am doing my assignment and have a question ,how can I clear the standard
> output(screen)?
>
> following is the fake code:
>


[...]

> the problem is,I want to clean the previous output number and then print
> only the new number .So it is like a on-screen counter somehow.


One way is to print out a lot of blank lines, so the old output will
disapaire, another way is using the preprozessor:
//do this in a seperate header file
#if defined LINUX
#define CLEAR_SCREEN clear
#elif defined WIN32
#define CLEAR_SCREEN cls
//for more platforms
#endif

or just spliting it into a front and a back end, by using in the backend
the platform specfic functions or libraries.

On Linux for example ncurouses and on windows for example the code that
David posted.

HTH && kind regards,
Nicolas

--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| http://www.velocityreviews.com/forums/(E-Mail Removed) | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Response.Clear() doesn't clear David ASP .Net 2 01-31-2008 08:32 PM
beginner Q: Kernel#puts, STDOUT, $stdout relation Andreas S Ruby 3 12-09-2006 12:39 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
copy stdout fails with permission denied when stdout is redirected brian.mabry.edwards@gmail.com Perl Misc 2 12-07-2005 10:49 PM



Advertisments