incorrect output

This is a discussion on incorrect output within the C Programming forums, part of the General Programming Boards category; im trying to get user input for a filename for opening later on. I know howto get the file open ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    24

    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;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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.
    My best code is written with the delete key.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Another method (as partly mentioned by Prelude):
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    24
    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

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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)
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String incorrect for output command target
    By DarkAlex in forum C++ Programming
    Replies: 16
    Last Post: 08-19-2008, 09:32 PM
  2. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 07:08 AM
  3. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM
  4. String output, sometimes incorrect
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2002, 07:46 AM
  5. Incorrect Output
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-07-2002, 08:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21