#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Chicago
    Posts
    14
    Rep Power
    0

    Question fscanf gives error - Help


    I am trying to read a space delimited text file using fscanf/fgets....

    does give a segmentation fault when runs.

    Here is the code.

    typedef struct {
    int acct_id;
    int domain_id;
    char domain[50];
    long sessionId;
    } dmSession;

    ...
    ...
    while( fgets(buff,sizeof(buff),dmptr) != NULL )
    {
    sscanf(buff,"%d %s %d\n", dmsessptr[i].acct_id, dmsessptr[i].domain, dmsessptr[i].domain_id);
    dmsessptr[i].sessionId = 0;
    ...
    ...

    and the text file looks like

    3337 .positiontech.com 4911
    3336 .positionpro.com 939
    1414 .ic.positiontech.com 4912
    7100 .iams.com 889
    7200 .eukanuba.com 904
    2394 .ascendant-group.com 1551
    4545 .grantasticdesigns.com 4913

    The error that comes when I debug is

    fstat64(3, {st_mode=S_IFREG|0644, st_size=1007, ...}) = 0
    mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40017000
    read(3, "3337 .positiontech.com 4911\n3336"..., 4096) = 1007
    --- SIGSEGV (Segmentation fault) ---
    +++ killed by SIGSEGV +++

    WHAT IS WRONG HERE!! I THINK IT TAKES THE NEXT LINE ALSO

    THANKS
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    What does your 'i' look like? You may be overrunning the size of your struct array. It is also quite possible that you have created the problem somewhere else in your program and only just starting to realize it (you could have a buffer overflow somewhere).

    BTW: Please enclose your code in "code" tags (http://forums.devshed.com/misc.php?action=bbcode&s=).

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Chicago
    Posts
    14
    Rep Power
    0

    commented code


    I comment out the code and it works fine. That means the problem is in fgets. i is declared as int i=0;
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    No it does NOT mean the problem is in the fgets. You can have a buffer overflow anywhere in your (previously executed) code and each time you recompile the program with even the most trivial of changes (adding a variable, commenting out a line) ANYWHERE in the code you can change the behavior.

    What value is i when it faults? What size is your buffer? If you are using buf as a pointer, sizeof(buf) will return the same thing as sizeof(char *) which is typically 4 bytes (but that is not likey to be the problem here).

    You are aware that sscanf (as well as it cousins) all require pointers? Perhaps that is your problem. Is dmsessptr statically allocated? If so, then change your sscanf to this:

    Code:
    sscanf(buff,"%d %s %d\n", &dmsessptr[i].acct_id, dmsessptr[i].domain, &dmsessptr[i].domain_id);
    dmsessptr[i].domain is already a pointer to a char array, so it doesn't need any changes. If this fixes your problem then welcome to the C club, it happens to everyone at least a dozen times or so.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo