Operating System - HP-UX
1834406 Members
1722 Online
110067 Solutions
New Discussion

how to print control character in a text file

 
prasad_15
Advisor

how to print control character in a text file

Hi,

How can I write a control character through a c program .

thanks
Prasad
10 REPLIES 10
A. Clay Stephenson
Acclaimed Contributor

Re: how to print control character in a text file

(void) printf("%c[B",0x1B);
(void) fflush(stdout);


You just sent ESC,'[','B' to stdout.
If it ain't broke, I can fix that.
prasad_15
Advisor

Re: how to print control character in a text file

Clay,

could you please let me know how can I send the control character for these function keys

F6
F7
F18

A. Clay Stephenson
Acclaimed Contributor

Re: how to print control character in a text file

It's not possible to answer your question easily because the function key sequences depend upon a) the TERM setting b) if function keys are defined in terminfo as set by TERM c) the keys may have been custom programmed so that they are unique to your particular terminal or terminal emulator.

Do this:
TERM=vt100 (or whatever)
export TERM
infocmp

Look for the kf6, kf7, and kf18 sequences (if defined).

Here's a simple method using the shell to do F6 -- which you could leverage using the system() function from within C.
KF6=$(tput kf6); echo "${KF6}\c"

Now the real C method is to use the curses tigetstr() function. You have to load the terminfo and then you can do:
char *F6;
F6 = tigetstr("kf6");
You have now captured the F6 sequence in a string and a simple
printf (or curses tparm command) will send it.

Man tigetstr for details and study curses because curses is one thing every UNIX programmer should at least have a little knowledge of. I have fired guys for hard-coding sequences into what was supposed to be portable code --- after they assured me that they were using terminfo.
If it ain't broke, I can fix that.
prasad_15
Advisor

Re: how to print control character in a text file

Clay,

When I did this
TERM=vt220
export TERM
infocmp


KF6=$(tput kf6);


echo "${KF6}\c"


the echo didnt produce anything .
Am I missing something?
Hein van den Heuvel
Honored Contributor

Re: how to print control character in a text file



> echo "${KF6}\c"
>
> the echo didnt produce anything .
> Am I missing something?

Yeah... the data is likely to be going in the wrong direction.
Are you per chance trying to 'pretent' an operator hit the KF6 key?

KF6 is a keystroke going from terminal to system. You are sending it to the terminal and it has no defined reaction for it.

If you could program a terminal to return key-strokes then this would normally be seen as a security violation.

close?

Regards,
Hein.

Elmar P. Kolkman
Honored Contributor

Re: how to print control character in a text file

The terminal has no way to display the keystroke. But the echo should give data. To see this, try to do it through a commando displaying Control/Escapes:
echo $KF6 | cat -tv

To do it from a C program, take a look at the (n)curses library.
Every problem has at least one solution. Only some solutions are harder to find.
A. Clay Stephenson
Acclaimed Contributor

Re: how to print control character in a text file

The infocmp command was just to let you see what the function keys actually send. It's not necessary for coding.

If you simply echo the characters to a terminal then it's likely that the terminal will interpret most of the sequence as control characters. If you want to actually see the characters then do this:
tput kf6 | od -c

Note that the kfN sequences are used by a curses enabled application to recogize that a particular key has been pressed. It's very unusual to actually want to send those characters from an application.
If it ain't broke, I can fix that.
prasad_15
Advisor

Re: how to print control character in a text file

All,

What I did to print F6 is
fprintf(f1," %c06\n",06);
to print F7
fprintf(f1," %c07\n",06);


Ramkumar Devanathan
Honored Contributor

Re: how to print control character in a text file

Prasad,

Try this key sequence in vi -

ctrl+V Esc

It used to work when i was working with teletypes and VT100 settings.

- ramd.
HPE Software Rocks!
A. Clay Stephenson
Acclaimed Contributor

Re: how to print control character in a text file

Well, that's wrong at least for a vt200, kf6=\E[17~ and kf7=\E[18~ (at least as verified by infocmp vt220 (or untic vt220).

You should really do this:
(void) fprintf(f1,"%c[17~",(char) 0x1B); /* kf6 */
(void) fprintf(f1,"%c[18~",(char) 0x1B); /* kf7 */
(void) fflush(f1);

and you are still not doing right even then because you asre not querying the terminfo database.



If it ain't broke, I can fix that.