overwriting in the terminal

J

James Leddy

Hello,

I need to write a program that will flash random numbers in the first line
of output. The first number flashed will only remain on the screen for
about 10 milliseconds. Then another number should appear in the same place
190 milliseconds later. I don't know how to approach the problem of
putting the standard output buffer back to the first point of output to
overwrite the number. I tried using fseek() on the standard output, but it
returned -1

I know that this can be accomplished. I just need a little bit of guidance.

Thanks for your help
 
J

Jack Klein

Hello,

I need to write a program that will flash random numbers in the first line
of output. The first number flashed will only remain on the screen for
about 10 milliseconds. Then another number should appear in the same place
190 milliseconds later. I don't know how to approach the problem of
putting the standard output buffer back to the first point of output to
overwrite the number. I tried using fseek() on the standard output, but it
returned -1

I know that this can be accomplished. I just need a little bit of guidance.

Thanks for your help

Not all files are seekable, particularly not those connected to
interactive, live human interface devices.

You could try something like:

fputs("string with first number", stdout);

....then to erase:

fputs("\r \r", stdout);

....where the second string has enough ' ' characters to overwrite the
first number, and whenever you are ready:

fputs("string with second number", stdout);

Might or might not work, depends greatly on your OS and terminal
driver.

You could replace the fputs() calls with printf() without a '\n', but
not puts(), which appends a newline automatically and moves to another
terminal display row, from whence there is no standard C method to
return.

You might also ask in a platform specific group, your compiler/OS
might provide its own extensions to make this easier.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
A

Avinash

Hello Try to use the following code it will help you
#include <stdio.h>
int main()
{
while (1 )
{
printf ( "\r|");
printf ( "\r-");

}
return 0;
}
 
T

Tristan Miller

Greetings.

I need to write a program that will flash random numbers in the first
line
of output. The first number flashed will only remain on the screen
for
about 10 milliseconds. Then another number should appear in the same
place
190 milliseconds later. I don't know how to approach the problem of
putting the standard output buffer back to the first point of output
to
overwrite the number. I tried using fseek() on the standard output,
but it returned -1

Interactive devices like terminals usually aren't seekable, so you may
encounter some problems there. A somewhat portable solution may be to
output the form-feed character, \f, to force the numbers to scroll off
the screen. However, you may find it easier to simply use some
compiler-specific extension to clear the screen. (In this case, you'll
need to ask on a newsgroup specific to your compiler or OS, since only
standard C is discussed here.)

Regards,
Tristan
 
S

Simon Biber

James Leddy said:
I need to write a program that will flash random numbers in
the first line of output. The first number flashed will only
remain on the screen for about 10 milliseconds. Then another
number should appear in the same place 190 milliseconds later.
I don't know how to approach the problem of putting the
standard output buffer back to the first point of output to
overwrite the number. I tried using fseek() on the standard
output, but it returned -1

fseek is totally the wrong approach. You want the carriage
return character.
I know that this can be accomplished. I just need a little bit
of guidance.

You have two problems; the first is possible in C, the second
one is impossible and therefore requires operating-system
specific calls to achieve.

1. There is a way to overwrite the output in C, limited to
overwriting a single line, but it doesn't always work.
2. There is no way to time intervals of 10 or 190 milliseconds
in C.

Here's some pseudocode, but you need to find a workable
implementation of my fictitious 'microsleep' function. In the
worst case you could try a busy loop and calibrate it to your
own system speed.

#include <stdio.h>

int main(void)
{
printf("%d", rand());
fflush(stdout);
microsleep(10);
printf("\r \r");
fflush(stdout);
microsleep(190);
printf("%d\n", rand());
return 0;
}

Remember the fflush(stdout) calls, they ensure that the
contents of the buffer are output to the screen even though
the line had not been finished yet.
 
M

Mansour Al-Aqeel

Simon said:
fseek is totally the wrong approach. You want the carriage
return character.




You have two problems; the first is possible in C, the second
one is impossible and therefore requires operating-system
specific calls to achieve.

1. There is a way to overwrite the output in C, limited to
overwriting a single line, but it doesn't always work.
2. There is no way to time intervals of 10 or 190 milliseconds
in C.

Here's some pseudocode, but you need to find a workable
implementation of my fictitious 'microsleep' function. In the
worst case you could try a busy loop and calibrate it to your
own system speed.

#include <stdio.h>

int main(void)
{
printf("%d", rand());
fflush(stdout);
microsleep(10);
printf("\r \r");
fflush(stdout);
microsleep(190);
printf("%d\n", rand());
return 0;
}

Remember the fflush(stdout) calls, they ensure that the
contents of the buffer are output to the screen even though
the line had not been finished yet.
I don't know exaclty what you wana do but If I have to write to the same
point of a treminal I would use ANSI escape sequences.
printf("\033[4G%d, any_number");
this will cause the output to be printed at location 4, then the next
will be at the same point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top