- ..clause 1..: Keyboard input by gets function
- ..clause 2..: Measures of overrunning in buffer
- ..clause 3..: Take out the numerical value etc. of the character string.
When inputting it from the keyboard by the scanf function before
It explained it was not able to know it because of the program even if there was an inputting error.
As measures, it inputs as a character string, and it explains the method of reading as the numerical value in a different way later.
The gets function that inputs the character string of one line from the keyboard is prepared in C language.
To use the gets function # include should do < stdio.h >.
The usage of the gets function is as follows.
If the gets function is executed, it becomes an input waiting state as well as the scanf function.gets(文字配列);
Moreover, it also explains the puts function that incidentally displays the character string of one line on the screen.
To use the puts function # include should do < stdio.h >.
The usage of the puts function is as follows.
The specified character string is displayed on the screen. Moreover, it changes line without fail at the end of the character string.puts(文字配列);
The following program is an example of displaying the character string that the user input as it is.
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { char str[32]; gets(str); puts(str); return 0; }
The input character string is displayed as it is.
Character string that inputs DRAGON QUEST 3
DRAGON QUEST 3
Apparently, there seems still will not to be character that the author became crooked soon.
When you explain how to become it if the array is passed to the function before
It explained that it was in the initial address of the array to be passed to the function, and was disregarded the number of elements.
Only the character array had been passed to the gets function ahead.
Then, the gets function is said no understanding of the number of elements of character arrays.
In a word, the input that exceeds the number of elements accepts, and causes the bug.
The bug occurs if 31 characters or more are input because the number of program elements ahead was 32.
The following execution result is an example of the input actually and seeing.
A familiar error screen is displayed, and has been canceled if it executes it.
0123456789012345678901234567890123456789 Input character string
The gets function cannot be used because there is such a problem.
The fgets function can be used.
To use the fgets function # include should do < stdio.h >.
The usage of the fgets function is as follows.
As understood from the specification of the file pointer at the endfgets(文字配列,配列の要素数,ファイルポインタ);
However, all peripherals can be actually handled as a file in C language.
To the keyboard The file pointer named stdin is allocated.
This The function read from the file changes quickly for the keyboard if stdin is specified.
It is easy and is certain to use the sizeof function to know the number of array elements.
[ ..about time.. equipment is a file treatment ].
The file treatment of peripherals In the function taken with OS named UNIX,
Almost all install a similar mechanism on a present computer.
Fgets (character array, sizeof (character array), and stdin);
The following program is an example of rewriting in a safe input method.
[ Line-feed character ]
Store the fgets function though the gets function doesn't store '\n'.
Retrieve '\n' to examine the end of the input.
The result of inputting a long character string by this program and testing safety is as follows.#include <stdio.h> int main(void) { char str[32]; fgets(str,sizeof(str),stdin); puts(str); return 0; }
It is understood to break off the input in the part that becomes limits of the number of elements, and to prevent the bug occurring.
0123456789012345678901234567890123456789 Input character string
0123456789012345678901234567890
The problem has been still left though the input of the character string is thorough in this.
It is a point that the numerical value cannot be input unlike the scanf function.
It is necessary to read the input character string in a different way as the numerical value.
The easiest method of reading the character string in a different way as the numerical value is to use the atoi function.
Already omit a detailed explanation because it has explained.
The following program is an example of displaying the second power of the input numerical value.
The execution result of this program becomes as follows.#include <stdio.h> #include <stdlib.h> int main(void) { char str[32]; int val; fgets(str,sizeof(str),stdin); val = atoi(str); printf("%d\n",val * val); return 0; }
Because the atoi function is disregarded even if things except the figure are included in the input character string
5
25
Though it is enough only if the numerical value is simply input by this
It wants to delimit two or more numerical values by the sign like the scanf function and to input it.
It is necessary to retrieve the sign at one's own expense, and to convert it into the numerical value respectively.
[ There is sscanf function, too ].
Though there is sscanf function that cuts out the numerical value from the character string, too
Do not treat here because there is a problem of looking like the scanf function.
Use the strtok function to take out the word from among the character string.
To use the strtok function # include should do < string.h >.
The usage of the strtok function is as follows.
/* address value = of */ word that takes out the first word Strtok (character array and delimiter); /* address value = of */ word that takes out the following word Strtok (NULL and delimiter); When the word is not found Return NULL.
The following program is an example of displaying the list of the input numerical value.
The execution result of this program becomes as follows.#include <stdio.h> #include <stdlib.h> #include <string.h> void main(void) { int i,j,val[10]; char str[32],*ch; fgets(str,sizeof(str),stdin); ch = strtok(str,",\n"); for (i = 0;i < 10;i++) { if (ch == NULL) { break; } else { val[i] = atoi(ch); } ch = strtok(NULL,",\n"); } for (j = 0;j < i;j++) printf("%d\n",val[j]); return; }
Enlarge the digit number for the figure, and input the character string.
85, 41, 26,956, and 12 Input character string
85
41
26
956
12