Compare the use of getc of function and ungetc of function in C language

  • 2020-04-02 03:19:39
  • 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 ungetc() function: returns a character to the input stream
The header file:


#include<stdio.h>

The ungetc() function is used to return a character to the input stream, which is retrieved by the next function that reads the file stream. The prototype is as follows:


  int ungetc(char c, FILE *stream);

[parameter] c is the character to be returned, and stream is the input stream to be returned.

[return value] if the function is executed successfully, it returns a non-zero value; Otherwise, 0 is returned.

Example: the following example demonstrates the use of the ungetc() function, which returns a character to the input stream, as shown below.


#include<stdio.h>
#include<ctype.h>
int main()
{
  int i=0;
  char ch;
  puts("Input an integer followed by a char:");
  //Read characters until an end character or non-numeric character is encountered
  while((ch = getchar()) != EOF && isdigit(ch))
  {
    i = 10 * i + ch - 48; //Converted to an integer
  }
  //If it is not a number, it is put back into the buffer
  if (ch != EOF)
  {
    ungetc(ch,stdin); //Returns a character to the input stream
  }
  printf("nni = %d, next char in buffer = %cn", i, getchar());
  system("pause");
  return 0;
}

Output results:


123ab � 
i *= 123, next char in buffer = a

The program starts executing the while loop until it encounters a non-numeric or end id and then proceeds to determine whether it is the end id or not. If it is not the end id, it returns to the keyboard buffer and USES getch() to retrieve the character output from the buffer at the end of the output. Because the function getchar() is used in while, you need to enter a character and press enter.


Related articles: