Thread: Inputting strings from a file

  1. #1
    UCF Mystic_Skies's Avatar
    Join Date
    Oct 2004
    Posts
    33

    Inputting strings from a file

    Hello everyone. I'm having a problem in a program I'm working on and I broke the program up into this small bit here. I've done searches on the forum and tried to input this file from what I've seen on other posts but what I cannot figure out is why the strings are not getting read correctly. Can anyone help?

    The error is that it's not printing the first 2 strings being read. The last (which is a char) gets read and prints out just fine strangely enough.

    Input:
    Code:
    PROGA      0000  C
    LISTB      0000  I
    ENDB       0000  I
    P          0048  R
    BR         104B  R
    B1         104E  R
    B2         1061  R
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    FILE *st_fptr, 
         *fopen();
    
    int main( void )
    {
       char temp_name[6];
       char temp_value_str[4];
       char temp_type;
       
       if ( ( st_fptr = fopen ( "symtab.txt", "r" ) ) == NULL )
       {
           printf ( "Can't open 'symtab.txt' file!\n" );
           getchar();
           exit(1);
       }  
    
       while ( !feof ( st_fptr ) )
       {
          fscanf ( st_fptr, "%s%s%s", temp_name, temp_value_str, &temp_type );
          printf ( "name: %s\n", temp_name );
          printf ( "string: %s\n", temp_value_str );
          printf ( "type: %c\n\n", temp_type );
       }  
       
       printf ( "Press return to continue..." );
       getchar();
       
       return 0;   
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    One thing I notice is that temp_value_str isn't large enough to hold the string. It needs to have at least 5 elements for there to be enough space.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Also, temp_type is a char, but you're using %s instead of %c.
    If you understand what you're doing, you're not learning anything.

  4. #4
    UCF Mystic_Skies's Avatar
    Join Date
    Oct 2004
    Posts
    33
    Hey thanks I changed the second string to hold 5, and %s to %c and now the first 2 strings display but the last one won't anymore. Strange.. I don't get that.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How about something like this?
    Code:
    #include <stdio.h>
    
    int main( void )
    {
       FILE *file = fopen ( "file.txt", "r" );
       if ( file )
       {
          char name[6], value[5], type[2];
          while ( fscanf ( file, "%5s%4s%1s", name, value, type ) == 3 )
          {
             printf ( "name:   %s\n",   name  );
             printf ( "string: %s\n",   value );
             printf ( "type:   %s\n\n", type  );
          }
       }
       return 0;
    }
    [edit]I chose a string for the third parameter because the %c directive is not whitespace-delimited like %s.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    UCF Mystic_Skies's Avatar
    Join Date
    Oct 2004
    Posts
    33
    Hey Dave. That solution looks like it will work but the only question I have is that I need "type" to just be a single char and you've defined it as an array of 2. Can fscanf read just a single char correctly?

    One more question also... If i'm reading in a string should I always put those numbers in between the % and the s the way you have shown?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Mystic_Skies
    Hey Dave. That solution looks like it will work but the only question I have is that I need "type" to just be a single char and you've defined it as an array of 2. Can fscanf read just a single char correctly?
    Yes.
    Code:
          char name[6], value[5], type;
          while ( fscanf ( file, "%5s%4s %c", name, value, &type ) == 3 )
          {
             printf ( "name:   %s\n",   name  );
             printf ( "string: %s\n",   value );
             printf ( "type:   %c\n\n", type  );
          }
    Quote Originally Posted by Mystic_Skies
    One more question also... If i'm reading in a string should I always put those numbers in between the % and the s the way you have shown?
    Yes.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    UCF Mystic_Skies's Avatar
    Join Date
    Oct 2004
    Posts
    33
    Thanks for the help. Problem solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Writing strings to file
    By Ichmael™ in forum C++ Programming
    Replies: 6
    Last Post: 07-12-2005, 12:37 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM