Comparison of the use of GETC () functions and UNGETC () functions in C language _c language

Source: Internet
Author: User
Tags numeric stdin

C language Getc () function: Reading characters from the stream
header file:

#include <stdio.h>

The function getc () is used to extract characters from the stream, and its prototype is as follows:

  int getc (FILE *stream);

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 Ungetc () function: Return the character to the input stream
header file:

#include <stdio.h>

The UNGETC () function is used to return a character to the input stream, and the returned character is obtained 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 the stream is the input stream to be returned.

Return value returns a value other than 0 if the function succeeds, otherwise, returns 0.

Example: The following example shows the use of the UNGETC () function, which returns characters to the input stream with the following code.

#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  char ch;
  Puts ("Input an integer followed by a char:");
  Reads a character until it encounters a terminator or a non-numeric character while
  ((ch = getchar ())!= EOF && isdigit (CH))
  {
    i = ten * i + ch-48;//Convert to Integer
  }
  If it is not a number, it is put back into the buffer if
  (ch!= EOF)
  {
    ungetc (Ch,stdin);//Send a character back to the input stream
  }
  printf ("\n\ni =%d, next Char in buffer =%c\n ", I, GetChar ());
  System ("pause");
  return 0;
}

Output results:

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

The

program begins to perform a while loop until it encounters a non-numeric or end identity to proceed, immediately after determining whether it is an end identity, or returning the keyboard buffer if it is not an end identity, and then using Getch () to retrieve the character output from the buffer at the last output. Because the function GetChar () is used in the while, you need to enter a character and press ENTER.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.