Download HiSoft C - sinclair
Transcript
int n;
char s (20);
scanf ("%d , %19s", &ni, s) ;
This will read an integer into n (&n points to n] and a string [which must not be longer than
the array s) into s. The two fields must be separated by a comma (and maybe some white
space). Note the use of the maximum width 19 to protect against an overlong input string.
Note that there was no ampersand (&) preceding s in the scanf call as b is already a pointer
to the array.
Notes on the use Of SCANF
scanf ( ) uses getchar ( ) to read characters and this is important to remember. getchar ( )
is a buffered function which doesn't pass across any input until the [enter]; My is pressed.
The [enter] key is always passed across after the rest of the line as a C NEWLINE character
\n. So. the scanf ( ) control string should almost always start with a space character to match
the NEWLlNE character left behind at the end of the previous line of input.
Don't put the space character at the end of the control string because then scanf ( ) will carry
on scanning until it finds something that isn't white space, before you have prompted for the next
input.
EOF handling is badly behaved; the safe thing to do is to call getchat ( ) after scanf ( ) In order
to see if you have reached end-of-file. if not you can use ungetc ( ) to put back the character you
read.
There is no %u conversion for scanf: %d is used for both cases Using a%u by mistake can
produce obscure symptoms because scanf tries to match against a u character, fails and
gives up early, which leaves junk in the remainder of the input variables
Int fscanf (fp,
control,
Int
control,
arg1,
arg2
...)
FILE *fp;
char *control;
This function behaves just like scanf except that its input is obtained from the file attached to fp
rather than stdin.
sscanf (s,
arg1,
arg2
...)
char *s; char * control;
This function behaves just like scanf except that it uses the string pointed to by s as its source
of input.
Raw-Level I/O Functions
Int rawin ( )
Inputs a character directly from the keyboard, with no conversion of character codes. There is no
cursor and nothing is echoed to the display. This function is intended for special applications
such as games. It waits for bit 5 of the FLAGS system variable to be set, then reads LAST_K and
resets the flag. This function is built-in.
HiSoft ZX C User Manual
Page 51