Search code examples
creadfile

Reading a file one char at a time


I have two .c files: one contains the main and the other contains all of my functions.

In my main I have a part where I want to read a char at a time the loop looks like this:

while(ch = ReadFile(fp)) != EOF)
{
    // Code
}

where ch is an integer (int ch;) and fp is a file pointer (FILE* fp;).

And (different .c file ---):

int ReadFile(FILE* fp)
{
    // Some code
    return n; // The next char
}

Does anyone know how I can read one char at a time from the main this way, using this method?


Solution

  • Update:-

    The famous getc from <stdio.h> is a solution:-

    while((ch = getc(fp)) != EOF)
        {
          //...
        } 
    ...
    int ReadFile(FILE* fp)
    {
       //Do what you wish... :)
      return getc(fp); 
    }
    

    Declared as int getc( FILE * _File);