Line overwriting

S

Steven Jones

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 previous two. The sleeping bit
is trivial but, what about the overwriting? I am assuming that the output
will be displayed on a standard terminal emulator, like xterm.
 
K

Kenny McCormack

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 previous two. The sleeping bit
is trivial but, what about the overwriting? I am assuming that the output
will be displayed on a standard terminal emulator, like xterm.

Allow me to be the first to say this - and I say it from the deepness of my
heart, with all the kindness and love one has come to associate with the
helpful posts you get in this newsgroup:

Not portable. Can't discuss it here. Blah, blah, blah.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Steven said:
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 previous two. The sleeping bit
is trivial but, what about the overwriting? I am assuming that the output
will be displayed on a standard terminal emulator, like xterm.

printf("\r"); fflush(stdout);


But, while this will work on most any display device (including hard copy
devices), it is likely to leave output in a mess on most of them.

- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)

iD8DBQFD8IgYagVFX4UWr64RAlVaAJsHIcbBegJsQf1pu669WsBbpAy1lwCg7RBb
bdLqgwKLNM3qDtTvOeU10Vc=
=tvu6
-----END PGP SIGNATURE-----
 
A

August Karlstrom

Steven said:
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 previous two. The sleeping bit
is trivial but, what about the overwriting? I am assuming that the output
will be displayed on a standard terminal emulator, like xterm.

As already mentioned, this is off topic in this newsgroup. You might
want to have a look at the NCURSES library.


August
 
E

Eric Sosman

August said:
As already mentioned, this is off topic in this newsgroup. You might
want to have a look at the NCURSES library.

The question seems entirely topical: "How can I do _X_
in C?" As it happens, the answer is either "You cannot"
(for strict constructionists) or "You need facilities that
are not part of C, but may be available as extensions on
the platforms that interest you" (for people of a helpful
bent). An *extremely* helpful person might even suggest a
look at Questions 19.3 and 19.4 in the comp.lang.c Frequently
Asked Questions (FAQ) list at http://c-faq.com/ -- but that's
*really* going too far, and anyone who did so would be rightly
castigated.

See also "praeterition."
 
K

Kenneth Brody

Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



printf("\r"); fflush(stdout);

But, while this will work on most any display device (including hard copy
devices), it is likely to leave output in a mess on most of them.

Given that he wants to overwrite both lines with two new lines[*], the
above solution won't work.

This comes down to the need to use some sort of extension, perhaps the
curses library, in order to do it. The use of curses (or any other
library which may perform this function) is OT in clc. I'm not sure
where curses is topical.


[*] As opposed to "two newlines". :)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
S

Steven Jones

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 previous two. The sleeping bit
is trivial but, what about the overwriting? I am assuming that the output
will be displayed on a standard terminal emulator, like xterm.

You guys are right: This is off-topic in this group. I realized shortly
after posting; my apologies. Anyway, in case anyone is interested, on an
xterm terminal emulator the effect can be achieved with

printf("\x1b[2A") ;

just before (or after) the sleep() call.
 
C

CBFalconer

Steven said:
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 previous two.
The sleeping bit is trivial but, what about the overwriting? I
am assuming that the output will be displayed on a standard
terminal emulator, like xterm.

You guys are right: This is off-topic in this group. I realized
shortly after posting; my apologies. Anyway, in case anyone is
interested, on an xterm terminal emulator the effect can be
achieved with

printf("\x1b[2A") ;

just before (or after) the sleep() call.

Whatever an "xterm emulator" may be, that code won't work unless
the destination falls in the class of ANSI standard CRT terminals
(which are OT here). These are only one of many possible terminal
types, and are the reason why such things as termtype and curses
(both also OT here) were invented.

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
 
F

Flash Gordon

Steven said:
You guys are right: This is off-topic in this group.

As Eric Sosman said, the question is on topic, it's just that the answer
is that you need something beyond standard C.
> I realized shortly
after posting; my apologies. Anyway, in case anyone is interested, on an
xterm terminal emulator the effect can be achieved with

printf("\x1b[2A") ;

just before (or after) the sleep() call.

For increased portability look at the various screen handling libraries
such as Curses as others have suggested.
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
 
M

Mark McIntyre

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 previous two. The sleeping bit
is trivial but, what about the overwriting? I am assuming that the output
will be displayed on a standard terminal emulator, like xterm.

This is a FAQ - search for entries relating to in-situ altering of
files - but essentially you can't do this in standard C.
Mark McIntyre
 
K

Keith Thompson

Mark McIntyre said:
This is a FAQ - search for entries relating to in-situ altering of
files - but essentially you can't do this in standard C.
Mark McIntyre

I think he's trying to overwrite text on the display, not data in a
file. (You can't do that in standard C either.)
 
M

Mark McIntyre

I think he's trying to overwrite text on the display, not data in a
file. (You can't do that in standard C either.)

Yes, indeed. I see that on rereading it. Its still a FAQ tho - a
search for gotoxy will turn up 19.4



Mark McIntyre
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top