C language Getc () function: Reading characters from the stream
header file:
The function getc () is used to extract characters from the stream, and its prototype is as follows:
The parameter argument *steam the file stream from which you want to read characters.
Return value returns the character that was read when the function was successfully executed.
Description reads one character from a file and then returns EOF when it reads to the end of the file without data. GETC () works the same as fgetc (), but in some libraries getc () is defined as a macro rather than a real function.
Examples below demonstrate the use of the GETC () function, which is used in a program to read characters from the standard input console, as follows.
#include <stdio.h>//introduction of standard input output library
void Main () {
char ch;
printf ("Input a Character:"); Enter hint info
ch = getc (stdin);//read characters from the standard input console
("The character input was: '%c ' \", ch);//output character
}
Run the above program to first declare a variable to hold the character you are taking, and then output the prompt to receive any key pressed from the standard input console and output the character to the console.
Use GETC () to read a string from a file, as shown in the following code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void)
{
int ch;
int Len;
int i=0;
file* FStream;
Char msg[100] = "hello! I have read this file. ";
Fstream=fopen ("Test.txt", "at+");
if (fstream==null)
{
printf ("Read file Test.txt failed!\n");
Exit (1);
}
/*getc reads a character from the file stream/while
(ch = getc (fstream))!=eof)
{
putchar (ch);
}
Putchar (' \ n ');
Len = strlen (msg);
while (len>0)/* Loop Write * *
{
putc (msg[i],fstream);
Putchar (Msg[i]);
len--;
i++;
}
Fclose (fstream);
return 0;
}
function fopen Use the pattern "at+" to open a text file, and use getc to read characters from the file stream one by one until it is finished.
C language Gets () function: Reading a string from a stream
Header file:
The gets () function is used to read a string from a buffer, and its prototype is as follows:
Char *gets (char *string);
The gets () function reads a string from the stream until a newline character or end of the file is read, with null ending as a string. The string being read is temporarily present in the given parameter string.
Return value returns a pointer to string if successful, otherwise null is returned.
Note: Because gets () does not check the size of string strings, you must encounter line breaks or end of file to end input, so it is easy to cause a security problem with cache overflow, causing the program to crash, and you can use Fgets () instead.
For example, see a simple example below.
#include <stdio.h>
int main (void)
{
char str[10];
printf ("Input a string.\n");
Gets (str);
printf ("The string you are:%s", str); Output all values, note a
}
If input 123456 (length is less than 10), the output is:
Input a string.
123456↙ the
string you input is:123456
If you enter 12345678901234567890 (length greater than 10), the output results are:
Input a string.
12345678901234567890↙ the
string you input is:12345678901234567890
Also see that the system hint program has crashed.
If the gets () function is not used correctly, the harm is great, as we see above, when the length of the input string is greater than the length of the buffer, there is no truncation, and the read string is exported as it is, causing the program to crash.
Considering the security and robustness of the program, it is recommended to use Fgets () instead of gets ().
If you use gets () in GCC, the compilation fails and prompts:
The ' gets ' function is dangerous and shout isn't used.
C language Gets () function: Reading a string from a stream
header file:
The gets () function is used to read a string from a buffer, and its prototype is as follows:
Char *gets (char *string);
The gets () function reads a string from the stream until a newline character or end of the file is read, with null ending as a string. The string being read is temporarily present in the given parameter string.
Return value returns a pointer to string if successful, otherwise null is returned.
Note: Because gets () does not check the size of string strings, you must encounter line breaks or end of file to end input, so it is easy to cause a security problem with cache overflow, causing the program to crash, and you can use Fgets () instead.
For example, see a simple example below.
#include <stdio.h>
int main (void)
{
char str[10];
printf ("Input a string.\n");
Gets (str);
printf ("The string you are:%s", str); Output all values, note a
}
If input 123456 (length is less than 10), the output is:
Input a string.
123456↙ the
string you input is:123456
If you enter 12345678901234567890 (length greater than 10), the output results are:
Input a string.
12345678901234567890↙ the
string you input is:12345678901234567890
Also see that the system hint program has crashed.
If the gets () function is not used correctly, the harm is great, as we see above, when the length of the input string is greater than the length of the buffer, there is no truncation, and the read string is exported as it is, causing the program to crash.
Considering the security and robustness of the program, it is recommended to use Fgets () instead of gets ().
If you use gets () in GCC, the compilation fails and prompts:
The ' gets ' function is dangerous and shout isn't used.