scanf problem

This is a discussion on scanf problem within the C Programming forums, part of the General Programming Boards category; scanf never seems to work properly for me when using strings, code below and sample input file also. Code: int ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    28

    scanf problem

    scanf never seems to work properly for me when using strings, code below and sample input file also.


    Code:
       int i = 0;
       FILE *file;
        
       file = fopen("playlist", "r");
       if ( file == NULL )
       {
          printf("\n\nPlaylist File Not Loaded!, Program Will Exit!\n\n\n");
          exit(0);
       }
      
      while (!feof(file))
      {
          fscanf(file,"%s %s %s %d",playlist[i].code,playlist[i].group,playlist[i].title,&playlist[i].no);
          i++;
      }
    sample input file

    KILL Killers Somebody_told_me 3
    GDAI Greenday American_idiot 2
    BD56 Bob_Dylan all_along_the_watchtower 3

    the output i get is for first one is
    code:KILLKillers group:Killers title:Somebody_told_me no:3

    as u can see all correct apart from the first one, ive tried putting %4s but still seems to add killers when it shouldnt do. can anyone help?

    cheers
    Last edited by a1dutch; 04-18-2005 at 05:10 PM.

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Code tags are enclosed with [ and ].
    fscanf discard white spaces....
    Check controling while loop in the FAQ
    Please post code for producing output, your code is not complete...
    Last edited by Micko; 04-18-2005 at 05:06 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    soz it was just a slip of the finger, i know fscanf does not count whitespaces but it should still let me split it up shouldnt it?

    The output was produced using a simple printf

    Code:
    printf("code:%s, group:%s, title:%s, no:%d", playlist[0].code,playlist[0].group,playlist[0].title,&playlist[0].no);
    i only printed the first output out, would just need putting in a loop to print the rest out
    Last edited by a1dutch; 04-18-2005 at 05:21 PM.

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    I made a little test with folloeing code and get output as expected
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      FILE* file;
      int i =0,no;
      char str1[BUFSIZ],str2[BUFSIZ],str3[BUFSIZ];
      file = fopen("test.txt","r");
      
      while(fscanf(file,"%s %s %s %d",str1,str2,str3,&no) ==4 )
      {
        i++;
      }
      fprintf(stdout,"%s %s %s %d",str1,str2,str3,no);
      fprintf(stdout,"\nTotal number of lines: %d",i);
      system("PAUSE");	
      return 0;
    }
    This little test works for me. Note that & is not needed in printf and check FAQ about using feof()

    Try printing playlist[0].code and group separately just to make sure everything is stored correctly...

    - Micko
    Last edited by Micko; 04-18-2005 at 05:31 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    still having problems with this

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct play
    {
      char code[4];
      char group[20];
      char title[30];
      int no;
    }; 
    struct play playlist[6];
    
    int main(void)
    {
       int i = 0;
       char line[80];
       FILE *file;
        
       file = fopen("playlist", "r");
       if ( file == NULL )
       {
          printf("\n\nPlaylist File Not Loaded!, Program Will Exit!\n\n\n");
          exit(0);
       }
    
       printf("\n\n");
       
       //read playlist and users into a seperate array
       while ( fgets(line, sizeof line, file) != NULL )
       {
         if ( sscanf(line,"%s %s %s %d", playlist[i].code, playlist[i].group, playlist[i].title, &playlist[i].no) == 4)
         {
           i++;
         }
       }
       //close the file
       fclose(file);
       
       for (i = 0; i < 7; i++)
       {
         printf("code:%s\tgroup:%s\ttitle:%s\tno:%d\n", playlist[i].code, playlist[i].group, playlist[i].title, playlist[i].no);
       }
       
       exit(0);
    }
    heres the output

    code:KILLKillers group:Killers title:Somebody_told_me no:3
    code:GDAIGreenday group:Greenday title:American_idiot no:2
    code:BD56Bob_Dylan group:Bob_Dylan title:all_along_the_watchtower no:3
    code:JSB2Von_Karajan group:Von_Karajan title:brandenburg_concerto_2 no:15
    code:AB03Acker_Bilk group:Acker_Bilk title:stranger_on_the_shore no:4
    code:FFTMFranz_Ferdinand group:Franz_Ferdinand title:Take_me_out no:4
    code:EM21Eminem group:Eminem title:Like_toy_soldiers no:5

    as u can see its taking both the code and the group in when i do sscanf, it also does it with fscanf. any ideas?
    Last edited by a1dutch; 04-19-2005 at 02:29 AM.

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    You're complicating too much.
    Please try this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    struct play
    {
      char code[20];
      char group[20];
      char title[30];
      int no;
    };
    
    struct play playlist[6];
    
    int main(int argc, char *argv[])
    {
      FILE* file;
      int i =0, no, k;
      file = fopen("test.txt","r");
      
      while(fscanf(file,"%s %s %s %d",playlist[i].code,playlist[i].group,playlist[i].title,&playlist[i].no) ==4 )
      {
        i++;
      }
    
      for (k = 0; k < i; k++)
      {
          printf("%s %s %s %d\n",playlist[k].code,playlist[k].group,playlist[k].title,playlist[k].no);
      }
      system("PAUSE");	
      return 0;
    }
    I thought you would use my code to write something like this.
    This works for me with this test file.
    You have some bugs in your code. You must have room for null character, otherwise you'll overwrite memory that you're not supposed to and that may crash your program.

    What I notice next is this:
    Code:
    for (i = 0; i < 7; i++)
    Are you sure you will always have 7 lines. Var "i" represents number of lines so why not use that information.
    One more thing you're defining array for only 6 elements of play list. Why? Think about it a little...

    - Micko
    Attached Files Attached Files
    Last edited by Micko; 04-19-2005 at 03:24 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    sorry i did try that but i think i found out what the problem was, am i right in thinking that chars dont start counting at 0 like arrays do? because i made code char[4] (assuming 0 1 2 3 4, including space for nullcharacter) so it should be char[5];

  8. #8
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    No,
    Code:
    char array_char[5];
    is nothing more different than
    Code:
    int array_int[5];
    Both will define array of 5 chars (integers).
    C-string is simply a character array ending with special zero character '\0'

    So if you want to store name a1dutch you will need space for 8 characters because of this:

    a 1 d u t c h \0

    thus you define array like this:

    Code:
    char name[8];
    And name [0] is character 'a', name[1] is '1' etc...

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    i was just being stupid counting 0 as first and only putiing 4 not 5. realize what ive done now but cheers for ur help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 05:15 PM
  2. Problem with scanf float..
    By aydin1 in forum C Programming
    Replies: 6
    Last Post: 03-06-2005, 12:35 PM
  3. scanf problem
    By gsoft in forum C Programming
    Replies: 3
    Last Post: 01-04-2005, 11:42 PM
  4. problem with looping scanf
    By crag2804 in forum C Programming
    Replies: 6
    Last Post: 09-12-2002, 08:10 PM
  5. scanf problem
    By Flikm in forum C Programming
    Replies: 2
    Last Post: 11-05-2001, 12:48 PM

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