Programming In C++

Download Report

Transcript Programming In C++

Programming In C++
Spring Semester 2013
Lecture 8
Programming In C++, Lecture 8 By Umer Rana
Extended Keyboard Codes
• As we know that keyboard usually generates the
ASCII codes for letters, numbers and punctuation.
Such keys all generate one-byte ASCII codes.
• However, there are a great many keys and key
combinations not represented by this one-byte
character set. For instance, the function keys, F1
to F10, are not represented by a single byte.
• That keys consists of two bytes; the first byte is 0
and the second byte is a number indicating the
particular key(Extended Code).
• Extended keyboard codes use two characters, the
first of which has an ASCII value of 0.
Programming In C++, Lecture 8 By Umer Rana
Normal v/s Extended Codes
97
Normal code 1 byte letter ‘a’
0
Extended code 2 bytes function key F1
59
Programming In C++, Lecture 8 By Umer Rana
Normal v/s Extended Codes
{
unsigned char key, key2;
while ( ( key=getch( ) )!= '\r' )
if (key==0)
{
key2=getch();
printf("%3d %3d\n", key, key2);
}
else
printf("%3d\n", key);
getche();
}
Programming In C++, Lecture 8 By Umer Rana
Cursor Control with ANSI.SYS
• Cursor control is achieved with ANSI.SYS using escape
sequences (a string of several special characters).
Void main (void)
{
while ( getche ( ) != ‘\r’) // Printer Character
printf(“\x1B[B”); // Cursor Down
}
• ALL ANSI.SYS commands begin with the escape
sequence, “\x1B[“
Programming In C++, Lecture 8 By Umer Rana
Cursor Control with ANSI.SYS
Code
Effect
“[2j”
Erase Screen & home Cursor
“[K”
Erase to end of line
“[A”
Cursor Up one row
“[B”
Cursor Down one row
Have many more codes see the Book Page 284
Programming In C++, Lecture 8 By Umer Rana
Cursor Control with Keyboard
As we know about extended character codes and cursor control, we
can put both ideas together to control the cursor with the arrow keys.
#define CLEAR “\x1B[2J”
#define C_LEFT “\x1B[D”
#define C_RITE “\x1B[C”
#define C_UP “\x1B[A”
#define C_DOWN “\x1B[B”
#define L_ARRO 75
#define R-ARRO 77
#define U-ARRO 72
#define D-ARRO 80
#define ACROSS 205
#define UPDOWN 186
Programming In C++, Lecture 8 By Umer Rana
Cursor Control with Keyboard
Void main (void)
{
Int key;
Printf(CLEAR);
While ( (key=getch() ) ==0)
{
Key=getch();
Switch (key)
{
case L_ARRO:
printf(C_LEFT); putch(ACROSS); break;
case R_ARRO:
printf(C_RITE); putch(ACROSS); break;
case U_ARRO:
printf(C_UP); putch(UPDOWN); break;
case D_ARRO:
printf(C_DOWN); putch(UPDOWN); break;
}
Printf(C_LEFT);
}
}
Programming In C++, Lecture 8 By Umer Rana
Cursor Control with Keyboard
putch():
This function is analogous to the getch() function, except that it prints
a character on the screen, rather than reading a character from the
keyboard.
Programming In C++, Lecture 8 By Umer Rana
Moving the Cursor to an Arbitrary Position
#define CLEAR “\x1B[2j”
#define ERASE“\x1B[K”
void main ()
{
int row=1,col=1;
while(row !=0)
{
printf(“\x1B[23;1f”);
printf(ERASE);
printf(“Type row & column number (from 10,40):”);
scanf(“%d,%d”,&row,&col);
printf(“\x1B[%d;%df”,row,col);
printf(“*(%d,%d)”,row,col);
}
}
Programming In C++, Lecture 8 By Umer Rana
Character Attributes
The “Attribute” of a Character describes its apperance:
e.g.Print in Bold,Nomal, blinking, UnderLine or in Reverse
Every character displayed on the screen is stored in the
computer’s memory as two bytes. One byte contains the
normal code for the character, while the other bytes contains
the character’s attribute.
Attribute of Character or String using to set ANSI.sys escape
sequence following the bracket, consists of a number, followed
by the letter ‘m’.
e.g. “\x1B[0m”
Programming In C++, Lecture 8 By Umer Rana
Character Attributes
0
1
4
5
7
8
Turn off Attribute,
Normal White on Black
Bold
Underline
Blinking
Reverse
Invisible
Programming In C++, Lecture 8 By Umer Rana
“\x1B[0m”
“\x1B[1m”
“\x1B[4m”
“\x1B[5m”
“\x1B[7m”
“\x1B[8m”
Command-Line Arguments
When we invoke the program from the operating system, we
can type not only the name of the program, but various other
items as well, such as the name of a file the application is to
work on.
C>wordproc letter.txt
“letter.txt” is used as a command line argument an argument
that appears on the command line.
Programming In C++, Lecture 8 By Umer Rana
Redirection
The MS-DOS operating system incorporates a powerful feature
that allows a program to read and write files even when this
capability has not been build into the program. This is done
through a process called Redirection.
Redirection provides an easy way to save the results of a
program; its use vaguely similar to that of the [Ctrl][PrtSc] key
combination to save program output to the printer, except that
the results can be sent to a disk file.
Similarly, Redirection can be used to read information from a
disk file directly into a program.
Output can be Redirected to go to a file instead of the screen;
input can be Redirected to come from a file instead of the
keyboard.
Programming In C++, Lecture 8 By Umer Rana
Redirection
Redirection Output
The Redirection output operator which is the greater than
symbol(>), causes any output intended for the screen to be
written to the file whose name follows the operator.
C:> programName > file.txt
The data to be redirected to a file does not need to be typed by
user, the program itself can generate it. Any output normally
send to the screen can be redirected to a disk file.
Programming In C++, Lecture 8 By Umer Rana
Redirection
Redirection Input
The Redirection input operator which is the Less than
symbol(<), causes redirect the input to a program so, instead of
reading characters from the keyboard the program reads them
from the file.
C:> programName < file.txt
Programming In C++, Lecture 8 By Umer Rana