-
incorrect output
im trying to get user input for a filename for opening later on. I know howto get the file open later but the input is all wierd characters after 8 on my computer and after 3 characters on my brothers. Please explain whats going on.
Thanks,
Mike
Code:
#define MAX_FILE_NAME 20
int main()
{
int i, a;
char filename[MAX_FILE_NAME]; /* full string */
char *realname;
printf("Filename you want too see: ");
for( i = 0; ( a = getchar()) != '\n'; i++)
filename[i] = a;
if ( i > MAX_FILE_NAME){
fprintf(stderr, "error: Filename is too long\n");
return 1;
}
if ( i <= 0){
fprintf( stderr, "error: Filename not entered\n");
return 1;
}
realname = &filename[0];
printf("%i\n", i);
printf("%s\n", realname);
return 0;
}
-
Don't forget that strings in C are terminated by the '\0' character. If you don't use a library function that handles this automatically, you need to add it yourself.
-
-
thanks guys i've got it figured out. Here is my first program i've made, don't laugh
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char buf[ BUFSIZ];
FILE *fp;
void print_file()
{
int a, i;
char output[BUFSIZ];
for( i = 0; ( a = fgetc(fp)) != EOF ; i++)
output[i] = a;
printf( "%s\n", output);
fclose( fp);
}
void open_file()
{
if ( ( fp = fopen( buf, "r")) == NULL){
fprintf( stderr, "sorry could not open file\n");
exit(1);
}
}
void get_filename()
{
char *p;
printf ( "Filename to print: ");
if ( fgets( buf, sizeof( buf), stdin) != NULL){
if ((p = strchr(buf, '\n')) != NULL)
*p = '\0';
}
}
void parse_args()
{
}
int main()
{
get_filename();
open_file();
print_file();
return 0;
}
Notice i'm still working on parsing arguments.
Mike
-
>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)