Reputation: 53
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");
}
Upvotes: 0
Views: 839
Reputation: 86
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");
}
Upvotes: 1
Reputation: 86
This is to the second question OG posted. The reason why you get garbage at the end of the output is because the last character of your input_file_array is not 0 (NULL value). This happens in two situations:
One is that the compiler (I'm not sure why) allows some garbage values after the input array to leak in. I would recommend having input_file_array be size 513 with input_file_array[512] always equal 0. That way, you don't ever need to deal with garbage.
The other situation is because your array is currently uninitialized. If you read less than 512 characters, the rest are still random values which appear as garbage when outputted. A good rule of thumb for strings is to just memset the array to 0 at the beginning. Another method would be to tack on the 0 to the element after your last read.
Upvotes: 0