473,511 Members | 16,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overwriting in the terminal

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
Nov 13 '05 #1
5 10418
On Tue, 11 Nov 2003 19:14:34 -0500, James Leddy
<jl*****@binghamton.edu> wrote in comp.lang.c:
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
Nov 13 '05 #2
Hello Try to use the following code it will help you
#include <stdio.h>
int main()
{
while (1 )
{
printf ( "\r|");
printf ( "\r-");

}
return 0;
}

James Leddy <jl*****@binghamton.edu> wrote in message news:<3f********@bingnews.binghamton.edu>...
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

Nov 13 '05 #3
Greetings.

In article <3f********@bingnews.binghamton.edu>, James Leddy wrote:
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

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Nov 13 '05 #4
"James Leddy" <jl*****@binghamton.edu> wrote:
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.

--
Simon.
Nov 13 '05 #5
Simon Biber wrote:
"James Leddy" <jl*****@binghamton.edu> wrote:
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.

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.

Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4054
by: flamesrock | last post by:
First, I'm very new to gui programming, so please go lightly on me :) Ok, so far I've settled on wxPython, and what I'd like to do as a first leap is *convert* a text program into a gui program....
3
5551
by: Josh Schmidt | last post by:
How can one set the terminal services profile and home directory path in AD using VB.NET? I can set the usual profile paths, but terminal services properties are not supported with ADSI. Any...
19
4474
by: tweak | last post by:
I have been messing around with buffers, and I found it peculiar that the code below will run without a segmentation fault. As far as I know, overwriting the allocated space from a call to...
8
2716
by: OHM | last post by:
Hi peoples, I dont know where to place this question, so as I know you guys are all resourceful experts, I thought I would try here. I am writing some code which will be run usingh remote...
20
10808
by: Joel Hedlund | last post by:
Hi all! I use python for writing terminal applications and I have been bothered by how hard it seems to be to determine the terminal size. What is the best way of doing this? At the end I've...
11
5498
by: Steven Jones | last post by:
I have a C program that prints out two lines as follows: Line 1 Line 2 What I would like is for this program to sleep for one second, and then print out two more lines, overwriting the...
3
1735
by: shawn | last post by:
In the past I have had SQL and Terminal server on the same PC. With SQL and Windows 2003 Terminal server, you can not have them on the same PC. How can I make this work with SQL and Terminal and...
7
3038
by: mike | last post by:
We have numerous Access 97 apps that we run on our Terminal Server, but two apps in particular is giving us a problem. The problem we are running into is when you select a drop down menu, it errors...
16
3973
by: =?Utf-8?B?RHdlZWJlcmVsbGE=?= | last post by:
I created an Access 2007 application for my customer. The application is shared by three employees on a server. It maintains a contact list including financial data and social security numbers. ...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.