Thread: a lil trouble with sscanf

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    9

    a lil trouble with sscanf

    so i have a text file that goes like
    cod1 last1, first1; 1 2 3
    cod2 last2, first2; 1 2 3
    .....
    using fgets in a loop to read line by line and then put each variable into a structure type using sscanf
    my structure type is
    Code:
    typedef struct
    {
         char code[5];
         char *name;
         int num[6];
    }PERSON;
    i was able to use sscanf just find to store cod1, cod2,... into char code but for some reason i can't get the name
    Code:
    while (i < 25)    {
            fgets(buffer, 99, file); //puts(buffer);
            sscanf(buffer, "%4s", pChar[i].code);
            printf("%s\n", pChar[i].code);
            pBuff = buffer + strlen(pChar[i].code) + 1;
            pChar[i].name = (char *)malloc(sizeof(char));
            sscanf(pBuff, "%[^;] %*c", pChar[i].name); <----error 
            printf("%s\n", pChar[i].name);
    
    
    
    
            i++;
    
    
        }
    i don't know what or how it went wrong and i've been looking back and forth but still don't get it ...
    output gives error after the first loop and there're some weird characters
    ╨H>
    cod1
    last1, first1
    cod2


    Process returned 255 (0xFF) execution time : 8.192 s
    Press any key to continue.
    Last edited by pcshano; 03-14-2013 at 12:10 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > pChar[i].name = (char *)malloc(sizeof(char));
    You need to allocate more than one char for the name you're reading in.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Quote Originally Posted by Salem View Post
    > pChar[i].name = (char *)malloc(sizeof(char));
    You need to allocate more than one char for the name you're reading in.
    what do you mean by that?
    because i declared in my structure it's just char *name; so shouldn't it be just (char*)?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > malloc(sizeof(char));
    is one char

    malloc(sizeof(char)*5);
    is 5 chars

    If your name is "Fred Flintstone",
    how many chars do you need?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Quote Originally Posted by Salem View Post
    > malloc(sizeof(char));
    is one char

    malloc(sizeof(char)*5);
    is 5 chars

    If your name is "Fred Flintstone",
    how many chars do you need?
    ic what you mean but if the names aren't the same
    like one only have 10 chars and another 12 chars
    so i should put the max chars allowed or should i use sizeof(pChar[i].name)?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well typically, you read the name into a large (say BUFSIZ) buffer to begin with, then you can measure the length and allocate space accordingly.

    You can also read a single char, reallocating an ever increasing buffer if you want to, but it's a lot of effort.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble reading integer from sscanf
    By Geekster in forum C Programming
    Replies: 2
    Last Post: 11-14-2012, 10:52 AM
  2. help with sscanf
    By khoavo123 in forum C Programming
    Replies: 2
    Last Post: 03-25-2012, 03:33 PM
  3. Sscanf
    By Dink87522 in forum C Programming
    Replies: 4
    Last Post: 10-05-2009, 08:36 AM
  4. sscanf()
    By task in forum C Programming
    Replies: 4
    Last Post: 11-22-2003, 04:43 PM
  5. sscanf help
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-15-2002, 09:56 PM