screen clearing in ANSI C

L

Leslie Kis-Adam

Mark said:
You know this is a FAQ, right?
I've found it out!

--
Leslie Kis-Adam or
Laszlo Kis-Adam or even
Kis-Ádám László

Student
Budapest University of Technology and Economics
Faculty of Electronic Engineering and Computer Science
<dfighter_AT_NOSPAM_freemail.hu>
 
J

Joel

Does anyone know, if it is possible to clear the screen in ANSI C?

On platforms that have "glass teletype" screens, it is usually
possible to send the terminal a "clear screen" message without
violating any of the rules on ANSI C.
If it is,then how?

Platform-specific escape codes, usually. On other platforms there will
be a command executable with the system() function.

If you meant "is it possible to *portably* clear the screen in ANSI
C?", the answer is no.

--Joel
 
J

Joel

Then your Lunix doesn't support ANSI escape codes

If you’re referring to lunix, then you ought to be using Ken’s C
compiler and not worrying about ANSI… ☺

--Joel
 
M

Malcolm

santosh said:
... and One Standard for C.
Oops, which one?
A failed standard is a huge problem.
The only answer is to be very conservative.
However the other day I found myself needing nans to represent uninitialised
numbers in my new BASIC interpreter. So I swallowed hard and put is an
_isnan() from Microsoft. Also I wanted a vnsprintf in some config file
reading code, soon to be posted.
The question is, is it better to have a potential security hole or a
potential unsupported dependency? Or should I add lines and lines of code
checking every argument, which invilves parsing a printf format string? Or
just make the interface harder to use?
 
S

santosh

Malcolm said:
A failed standard is a huge problem.
The only answer is to be very conservative. ....
The question is, is it better to have a potential security hole or a
potential unsupported dependency?

In nearly all cases the latter is better, particularly if you clearly
document the dependency. It shouldn't be too much of a problem for
users of the program to download the correct version as well as any
dependencies.
Or should I add lines and lines of code
checking every argument, which invilves parsing a printf format string? Or
just make the interface harder to use?

Your call, (and I suppose it'll vary on a case by case basis,
especially depending upon the program's target user base's skill
level), but the trend for a decade now has been to make the software
"user friendly", at the cost of the programmer and portability.
 
C

Chris Hills

Then your Lunix doesn't support ANSI escape codes

http://en.wikipedia.org/wiki/ANSI_escape_code


This is where Wiki gets dangerous. Any damned fool can write it.

Linux, Unix, Dos, windows, VMX, VAX, CPM , MPM, OS9, PSOS etc etc have
terminal windows. (Even if in the case of DOS it is only one large
terminal window)

They use terminal drivers. Two of the more common are VT100 and VT52.
However there are many others.

What is the terminal type you are trying to clear screen on?

BTW this is WAY off topic for C.L.C
Richard said:
(e-mail address removed) said:
Leslie Kis-Adam wrote:
Hi everyone!
Does anyone know, if it is possible to clear the screen in ANSI C?
If it is,then how?
Any help would be appreciated.

Laszlo Kis-Adam
<dfighter_AT-NOSPAM_freemail.hu

printf(" \033[2J");

should do the work, it's an ansi code for clear screen

I wrapped it up in a main and ran it on my Linux box, but the screen didn't
clear. Not even the terminal window cleared.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
C

Chris Hills

(e-mail address removed) wrote:

Your reply should ideally be in between or after the quoted text, as
otherwise, it affects the reading order for other posters.
Richard said:
(e-mail address removed) said:
Leslie Kis-Adam wrote:
Hi everyone!
Does anyone know, if it is possible to clear the screen in ANSI C?
If it is,then how?
Any help would be appreciated.

Laszlo Kis-Adam
<dfighter_AT-NOSPAM_freemail.hu

printf(" \033[2J");

should do the work, it's an ansi code for clear screen

I wrapped it up in a main and ran it on my Linux box, but the screen didn't
clear. Not even the terminal window cleared.

Then your Lunix doesn't support ANSI escape codes

http://en.wikipedia.org/wiki/ANSI_escape_code

Nor does standard C. Why make your program needlessly non-portable,
especially given that clearing the screen is not important to most
console programs?

If yopu have a screen you will need to clear it at some point. You have
to use the escape codes for that VT

What to you meed by "needlessly non-portahble? The only way o0f making
it completely portable is having no screen.
 
T

Thomas Ploch

santosh said:
Your call, (and I suppose it'll vary on a case by case basis,
especially depending upon the program's target user base's skill
level), but the trend for a decade now has been to make the software
"user friendly", at the cost of the programmer and portability.

"user friendly" went from meaning to have a powerful tool that you need
to learn to use to having a somewhat "has to work out of the box
without even understanding what it does".
I would rather say "user knowledge overriding" software.

Thomas
 
K

Kevin Handy

Malcolm said:
What I need is a function that will clear my desk.

Take the mechnism from one of those self-cleaning
kitty-litter boxes, and mount it to your desk.
It will then automatically clear your desk after
each use.
 
M

Mark McIntyre

If yopu have a screen you will need to clear it at some point.

Really? If I did that, all the icons, mouse pointer, menu and desktop
would vanish and I'd be unable to use my pc any more.
What to you meed by "needlessly non-portahble? The only way o0f making
it completely portable is having no screen.

Or not clearing it.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Mark McIntyre

That is not a"clear screen."

Yes it is.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
B

Ben Pfaff

David T. Ashley said:
I don't know about ANSI ... but in the *nix world most shell programs
emulate the VT100 ... the following program will clear the screen.

This is exactly why off-topic questions should not receive
answers: often, the given "answers" are wrong.

In the Unix world, the shell has nothing to do with terminal
emulation.
 
D

David T. Ashley

Leslie Kis-Adam said:
Hi everyone!
Does anyone know, if it is possible to clear the screen in ANSI C?
If it is,then how?
Any help would be appreciated.

I don't know about ANSI ... but in the *nix world most shell programs
emulate the VT100 ... the following program will clear the screen.

#include <stdio.h>

char s[] = {27, '[', '2', 'J', 0};

int main(void)
{
printf("%s", s);
}

A list of some commands is here:

http://www.cs.utk.edu/~shuford/terminal/vt100_reference_card.txt

Searching by VT100 will turn up more.
 
R

Richard Tobin

I don't know about ANSI ... but in the *nix world most shell programs
emulate the VT100 ... the following program will clear the screen.
[/QUOTE]
In the Unix world, the shell has nothing to do with terminal
emulation.

I think that was just a mistake of terminology, rather than being
substantively wrong. After all, Sun's first terminal emulator was
called "shelltool".

I am somewhat suspicious of a program that wants to clear the screen.
If it wants to take over the whole screen, perhaps for editing, then
it's probably going to need a lot more than screen-clearing. If it
isn't doing that, then is it *really* necessary to clear the screen?
Too many programs do it unnecessarily, erasing possibly useful
information for the sake of neatness. So I would advise the OP to
consider whether it's really needed.

-- Richard
 
J

Joe Wright

David said:
Leslie Kis-Adam said:
Hi everyone!
Does anyone know, if it is possible to clear the screen in ANSI C?
If it is,then how?
Any help would be appreciated.

I don't know about ANSI ... but in the *nix world most shell programs
emulate the VT100 ... the following program will clear the screen.

#include <stdio.h>

char s[] = {27, '[', '2', 'J', 0};

int main(void)
{
printf("%s", s);
}

No. Unix shells do not emulate terminals. None of sh, ksh or csh knows
about terminals. You have to tell them through systems such as termcap
or terminfo.
 
D

David T. Ashley

Ben Pfaff said:
This is exactly why off-topic questions should not receive
answers: often, the given "answers" are wrong.

Non-sequitur: even if the question were on-topic, there is no guarantee I
would have answered it correctly.

Additionally, my answer is "right". The short program will clear the screen
using Putty and probably also using SSH Secure Shell.

I also pointed the original poster to VTXXX materials ... a starting point.

I'm not sure there is a single method that will clear the screen on all
platforms.
In the Unix world, the shell has nothing to do with terminal
emulation.

That is like claiming that traffic lights have nothing to do with
automobiles ...

The shell (bash or similar) usually [ultimately] interacts with the user via
some kind of terminal emulator ... terminal emulation comes into it quite
quickly unless you only output and input sequential lines of text.

http://en.wikipedia.org/wiki/Unix_shell
 
W

Walter Roberson

Additionally, my answer is "right". The short program will clear the screen
using Putty and probably also using SSH Secure Shell.
The shell (bash or similar) usually [ultimately] interacts with the user via
some kind of terminal emulator ... terminal emulation comes into it quite
quickly unless you only output and input sequential lines of text.

Putty contains a terminal emulator, but it is not a shell -- it does
not *itself* parse commands and submit them for execution.
Putty accepts characters and sends them to somewhere else for processing,
and handles received characters, but it is only displayware.

bash and similar may usually interact with a user via some kind of
terminal emulator, but bash etc. do not -provide- that terminal emulator:
they blindly make use of whatever information they have been told about
the characteristics of the terminal emulator that some other layer has
provided.

For example, right at the moment, I am typing this message within
the vi editor (not a terminal emulator), which was invoked by trn
(not a terminal emulator), which I commanded to start from within ksh
(not a terminal emulator), which transmits and receives data over a
pair of network sockets (not terminal emulators) that were created
between a remote rlogind server and a local rlogin client; the rlogin
client was something I commanded to start from within ksh (not a terminal
emulator), which transmits and receives data via an X Windows session
(which knows how to draw pretty graphics but needs to be told where
and what to draw); that X Windows session is being managed by the
4Dwm window manager (which knows more about where to draw various kinds
of things), within which I have started up an Xwsh window that has the
ksh running within it -- and the Xwsh window *is* the terminal emulator.

To recap that, there is something that knows about graphics (X) and
something that knows about styles (4Dwm), and there is a terminal
emulator, Xwsh, which is connected to ksh that I told to rlogin to
another machine, and on the ksh on the other machine I invoked trn
which started up vi which is sending the screen-clear and so on needed
way back at the Xwsh program.

Notice that none of the shells or rlogin (or ssh if I were using that)
are terminal emulators: a different piece entirely was the terminal
emulator -- a piece that would correspond most closely to the Putty
you mentioned. Notice the terminal emulator Xwsh is not any of the
several layers of shells, nor is the terminal emulator any of the
network transport layers.


So... you *were* wrong, shell programs do not emulate any
terminal, and are not responsible for what appears (or not) on the
screen.
 
K

Keith Thompson

Leslie Kis-Adam said:
Hi everyone!
Does anyone know, if it is possible to clear the screen in ANSI C?
If it is,then how?
Any help would be appreciated.

I'm amazed. I see 57 articles in this thread. Only one mentions that
this is a FAQ, and that one didn't say which one.

We have a very good FAQ list for this newsgroup. We should use it,
rather than reinventing the wheel. (And if we must reinvent the
wheel, can we at least make it round?)

The comp.lang.c FAQ is at <http://www.c-faq.com/>. You have asked
question 19.4.

I have a couple of comments on top of that.

The FAQ overstates the portability of using a form-feed character,
'\f', to clear a screen; it doesn't work on most of the terminal
emulators I use.

Consider carefully whether you really need to clear the screen at all.
You'll be erasing information belonging to whomever is using your
program, information that is probably on the screen for a very good
reason. There are good reasons for clearing the screen, but be sure
you actually have a good reason before you do it. (If your program
needs to control the entire screen, like a text editor, you'll need
something much more sophisticated than a control sequence or simple
external program; the details are equally off-topic.)
 
J

J. J. Farrell

Chris said:
That is not a"clear screen."

It's not a banana either, so what? It's about the best (or only) method
available to portably do what the OP requested, since it covers most
cases (depending on detail of what was meant by "clear" and "the
screen", but it works for most common meanings of those terms).
 

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
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top