C language getc of function and gets of function of the use of comparison

  • 2020-04-02 03:19:08
  • OfStack

C getc() function: reads a character from a stream
The header file:


#include <stdio.h>

The function getc() is used to fetch characters from the stream, with the following prototype:


  int getc(FILE *stream);

Parameter *steam is the stream of files from which the characters are to be read.

On success, this function returns the character read.

If a character is read from a file, EOF is returned when the end of the file is read and there is no data. Getc () works the same as fgetc(), but in some libraries getc() is defined as a macro, not a real function.

The following example demonstrates the use of the getc() function, which is used in the program to read characters from the standard input console, as shown below.


#include <stdio.h> //Introduce the standard I/o library
void main( ) {
  char ch;
  printf ("Input a character: ");  //Enter prompt information
  ch = getc(stdin); //Reads characters from the standard input console
  printf ("The character input was: '%c'n", ch); //The output characters
}

To run the program, first declare a variable to hold the character. The prompt is then typed, any key pressed from the standard input console is received, and the character is printed to the console.

Read the string from the file with getc(), as follows.


#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);
  }
  
  while( (ch = getc(fstream))!=EOF)
  {
    putchar(ch);
  }
  putchar('n');
  len = strlen(msg);
  while(len>0)
  {
    putc(msg[i],fstream);
    putchar(msg[i]);
    len--;
    i++;
  }
  fclose(fstream);
  return 0;
}

The function fopen opens a text file with the pattern "at+" and USES getc to read characters one by one from the file stream until it is finished.

C gets() function: reads a string from a stream
The header file:


 #include <stdio.h>

The gets() function is used to read a string from the buffer and has the following prototype:


  char *gets(char *string);

The gets() function reads the string from the stream until either a newline character appears or the end of the file is read, ending with NULL. The read string is temporarily stored in the given parameter string.

Returns a pointer to the string on success, otherwise NULL.

Note: because gets() does not check the size of the string, you must encounter a newline or file end to end the input, which can easily lead to a cache overflow security problem, resulting in a program crash, you can use fgets() instead.

Take a look at the following simple example.


#include <stdio.h>
int main(void)
{
  char str[10];
  printf("Input a string.n");
  gets(str);
  printf("The string you input is: %s",str);  //Output all the values, notice a
}

If the input is 123456 (length less than 10), the output is:


Input a string.
123456 � 
The string you input is:123456

If the input is 12345678901234567890 (length greater than 10), the output is:


Input a string.
12345678901234567890 � 
The string you input is:12345678901234567890

At the same time, the system prompts that the program has crashed.

If you don't use the gets() function correctly, the damage can be significant. As we saw above, when the input string is longer than the buffer length, it is not truncated, but the read string is printed as it is, causing the program to crash.

For program safety and robustness, it is recommended that fgets() be used instead of gets().

If you use gets() in GCC and the compilation fails, you will be prompted:


the 'gets' function is dangerous and shout not be used.

C gets() function: reads a string from a stream
The header file:


 #include <stdio.h>

The gets() function is used to read a string from the buffer and has the following prototype:


  char *gets(char *string);

The gets() function reads the string from the stream until either a newline character appears or the end of the file is read, ending with NULL. The read string is temporarily stored in the given parameter string.

Returns a pointer to the string on success, otherwise NULL.

Note: because gets() does not check the size of the string, you must encounter a newline or file end to end the input, which can easily lead to a cache overflow security problem, resulting in a program crash, you can use fgets() instead.

Take a look at the following simple example.


#include <stdio.h>
int main(void)
{
  char str[10];
  printf("Input a string.n");
  gets(str);
  printf("The string you input is: %s",str);  //Output all the values, notice a
}

If the input is 123456 (length less than 10), the output is:


Input a string.
123456 � 
The string you input is:123456

If the input is 12345678901234567890 (length greater than 10), the output is:

Input a string.
12345678901234567890 � 
The string you input is:12345678901234567890

At the same time, the system prompts that the program has crashed.

If you don't use the gets() function correctly, the damage can be significant. As we saw above, when the input string is longer than the buffer length, it is not truncated, but the read string is printed as it is, causing the program to crash.

For program safety and robustness, it is recommended that fgets() be used instead of gets().

If you use gets() in GCC and the compilation fails, you will be prompted:


the 'gets' function is dangerous and shout not be used.



Related articles: