cfilepointersscanfgetc

How to read a file and use getc in C?


I am a senior Comp Sci major about to enter my last semester where I will be taking just one class involving only the language C. I was attempting to practice my skills by making a rather simple program that I conceived of. I simply want to read a single file, put its entire contents into an array, and then traverse this array looking for a sequence of characters that spells out "Waldo". In other words, a sort of Where's Waldo? program.

I have not gotten into coding the "Waldo" search with the array yet, but instead, I simply just wanted to test my use of the getc() function and output the input file's contents, but with the source code I have so far, I only receiving incorrect outputs of seemingly gibberish (probably having to do with the way I am incorrectly utilizing the getc() return value).

Maybe I should not be using getc() and instead be using a scanf() type function? Also, I very much am trying to master the by-reference pointer-notation when inserting into and printing out the array here, so I would like to avoid referring to array elements using bracket notations (by-value). Admittedly so, I may not have the most complete understanding of pointers.

#include <stdio.h>
#include <stdlib.h>

void main( int argc, char *argv[] )
{

    FILE *filePointer;
    int filechar, i; 
    char input_file_array[512];
    char *arrayPointer = input_file_array;
    filePointer = fopen( argv[1], "r" );

    if ( argc > 2 || ( filePointer = fopen( argv[1], "r" ) ) == NULL )
    {
        printf( "\nIncorrect usage, please say...\nRunWaldo *filename*\n\n" );
        exit( 1 );
    }

    if ( filePointer != NULL )
    {    
        while ( !feof( filePointer ) )
        {
            *arrayPointer = getc( filePointer );
            arrayPointer++;
        }

    for ( i = 0; i < 512; i++ )
    {
        printf( "%c", *(arrayPointer + i) );
    }

    printf("\n");

}

Here is the output...


Solution

  • The major issue in your code is actually how you printed out the input_file_array. Imagine you are reading 10 characters from file. arrayPointer is now offsetted by 10 from the original position of input_file_array. Afterwards, you are not resetting arrayPointer back to input_file_array[0].

    So it starts printing from input_file_array[10] and onwards. And an even larger problem happens when you output input_file_array[512+10] because you have officially gone out of bounds and some funky things will happen. What you should do is this:

    #include <stdio.h>
    #include <stdlib.h>
    
    void main( int argc, char *argv[] )
    {
    
        FILE *filePointer;
        int filechar, i; 
        char input_file_array[512];
        char *arrayPointer = input_file_array;
        filePointer = fopen( argv[1], "r" );
    
        if ( argc > 2 || ( filePointer = fopen( argv[1], "r" ) ) == NULL )
        {
            printf( "\nIncorrect usage, please say...\nRunWaldo *filename*\n\n" );
            exit( 1 );
        }
    
        if ( filePointer != NULL )
        {    
            while ( !feof( filePointer ) )
            {
                *arrayPointer = getc( filePointer );
                arrayPointer++;
            }
        }
    
        arrayPointer = input_file_array; // or you can just output input_file_array[i] instead
        for ( i = 0; i < 512; i++ )
        {
            printf( "%c", *(arrayPointer + i) );
        }
    
        printf("\n");
    
    }