>Here is my first program i've made, don't laugh
Why does everyone seem to think we will make fun of them?
Code:
for( i = 0; ( a = fgetc(fp)) != EOF ; i++)
output[i] = a;
printf( "%s\n", output);
fclose( fp);
Two things wrong with this code. First is the indention. Seeing a loop with a multiple line body and no braces sends up warning flags. Upon further study, the code is correct, but the indention doesn't suggest this. Next, what if the file doesn't contain your nul termination character? A better loop would be:
Code:
for ( i = 0; i < BUFSIZ - 1 && ( a = fgetc ( fp ) ) != EOF; i++ )
output[i] = a;
output[i] = '\0';
if ( ferror ( fp ) )
perror ( "A read error occured" );
else
printf ( "%s\n", output );
fclose ( fp );
>fprintf( stderr, "sorry could not open file\n");
This says the file couldn't be opened, but not why. The perror function would be better as it also gives you a more descriptive error:
Code:
if ( ( fp = fopen( buf, "r")) == NULL){
perror ( NULL );
>exit(1);
exit only has three standard arguments, 0, EXIT_SUCCESS, and EXIT_FAILURE. The latter two are macros defined in stdlib.h. Unless you are actually using an implementation specific return code (which should be noted with a comment), you should stick to only those three.
>void print_file()
>void open_file()
>void get_filename()
>void parse_args()
A function that takes no arguments in C is noted with void. This isn't as much of an issue unless you use prototypes, but only the most trivial of programs don't. Your function tags should be:
Code:
void print_file(void)
void open_file(void)
void get_filename(void)
void parse_args(void)