Code:
#include <stdio.h>
int main( int argc, char* argv[] )
{
// reserve space for the string to be input
char *filename = malloc( sizeof( char ) * 10 );
char c; // individual characters found
printf( "Enter filename: " );
scanf ( "%s", filename );
FILE *fp; // pointer to FILE type(declared in stdio.h)
fp = fopen( filename, "r" ); // open the file and read
// perform error checking if file is not found
if ( !fp ) {
printf( "Error reading file.\n" );
return 0;
}
// aesthetics
printf( "\nFile name: %s\n", filename );
printf( "Output:\n" );
putchar( '\n' );
while ( fscanf( fp, "%c", &c ) != EOF ) {
c = tolower( c ); // change every uppercase to lowercase
printf( "%c", c ); // simply output the character
if ( c == ' ' ) { // if character encountered is a space
putchar( '\n' ); // then output a newline character
}
}
fclose( fp ); // close the file when finished
return 0; // normal program termination
}
You allocated 10 chars for filename, then used scanf() without bounds. . . on this board this is considered "bad".
You are using the C99 mixed declarations. . . not wrong, but not common.
fscanf() returns the number of successfull items read, not NULL. Should use something like fgetc().
malloc is *usually* defined in stdlib.h, which is not included.
Also, some on this board will tell you that sizeof(char) is redundant.
Salem, did I miss anything?