Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
C++ Tutorial
C++ Language Tutorial - Object Oriented Programing - OOPs History of C++
C++ Language Tutorial - Object Oriented Programing - OOPs Structure of C++
C++ Language Tutorial - Object Oriented Programing - OOPs C++ Datatypes
C++ Language Tutorial - Object Oriented Programing - OOPs Variables
C++ Language Tutorial - Object Oriented Programing - OOPs Constants
C++ Language Tutorial - Object Oriented Programing - OOPs Operators
C++ Language Tutorial - Object Oriented Programing - OOPs Control Structures
C++ Language Tutorial - Object Oriented Programing - OOPs Arrays
C++ Language Tutorial - Object Oriented Programing - OOPs Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Class
C++ Language Tutorial - Object Oriented Programing - OOPs Predefined Functions
C++ Language Tutorial - Object Oriented Programing - OOPs I/O Functions
C++ Language Tutorial - Object Oriented Programing - OOPs String, Character Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Mathematical Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Time Date Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Dynamic Allocation
C++ Language Tutorial - Object Oriented Programing - OOPs Utility Functions
C++ Language Tutorial - Object Oriented Programing - OOPs OOP's Concept
C++ Language Tutorial - Object Oriented Programing - OOPs Special Topics
C++ Language Tutorial - Object Oriented Programing - OOPs Type casting
C++ Language Tutorial - Object Oriented Programing - OOPs Feedback
C++ Language Tutorial - Object Oriented Programing - OOPs Ask Your Doubts
 




getc() - I/O Function


Tutorials Cpp

Topic

How is "getc()" used in C++?



Explanation

getc() is an I/O function equivalent to fgetc() also expects a stream as parameter, but getc may be implemented as a macro. This function is used to return the next character from the input stream then increases the file position indicator. The character read by this function is a unsigned char which is converted to an integer. If the End-of-File is reached or a reading error occurs the function returns EOF.

Syntax:
    int getc (FILE *stream);
Example:

   #include <stdio.h>
   int main()
    {
      int c;
      int count;
      FILE *stri = fopen("getceg.txt", "r");
       while( c != EOF ) 
        {
           c = getc(stri); 
            if (c == 'A') count++;
        }
      fclose(stri);
      printf ("File contains %dA.\n", count);
      return 0;
    }

Result:
    File has the character A 3 times.

In the above example the function is used to get every character of a text file till EOF. A loop is used to find the number of times the char 'A' is found in the file. The content of the text file is "ABCA1@@ABNM".










Note
C++ is one of the most used programming languages in the world. Also known as "C with Classes". Hope you enjoy this tutorial. Do send your feedback or suggestions on this C++ tutorial. This is a copyright content.

Other Links

web hosting