fscanf causing a crash

This is a discussion on fscanf causing a crash within the C Programming forums, part of the General Programming Boards category; I am using a simple file format, so I want to use fscanf to read the items in the file. ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    10

    Question fscanf causing a crash

    I am using a simple file format, so I want to use fscanf to read the items in the file.

    The format:

    a string of words
    an_integer<tab>another_integer
    an_integer<tab>another_integer

    and so on...


    I get the first line fine with fgets, and then the second line with:

    fscanf (infile, "%d", &int1);
    fscanf (infile, "%d", &int2);

    But I cannot get the third by repeating this code. How can I get to the new line?

    Thanks for any help.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Why not just get all the lines with fgets() and parse them with sscanf() ?

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    10
    Working pretty smoothly now, except the last line where I get a bunch of integers from the same line using:

    Code:
            for (i=0; i<theListSize; i++)
                sscanf(theLine, "%d", &theList[i]);
    I read the first integer over and over.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you would do. You could use strtok() or some such, or if you know how many integers you could have on the line, you can use something like:
    Code:
        x = sscanf("%d %d %d %d", &theList[i], &theList[i+1], &theList[i+2], &theList[i+3]);
        i += x-1;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    10
    Shouldn't this code be able to fetch me all the integers on a line containing:

    "1 2 3 4 5 6 7 8 9 10 11 12" without quotes?

    Code:
            char * pch;
            pch = strtok(theLine, " ");
            while (pch!=NULL)
            {
                  printf ("&#37;s\n",pch);
                pch = strtok(theLine, " ");
            }
    It gets me the first integer, 1, and repeats forever.
    Last edited by dougwilliams; 11-18-2007 at 04:48 PM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dougwilliams View Post
    I am trying strtok() but I get the same problem -- it is only reading the first integer on the line over and over and does not reach NULL..

    Code:
            char * pch;
            pch = strtok(theLine, " ");
            while (pch!=NULL)
            {
                  printf ("%s\n",pch);
                pch = strtok(theLine, " ");
            }
    Ehm, strtok should be given "NULL" if you want to continue finding from where it last started - or you could use "pch".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    10
    Oops! Works like a charm, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-29-2008, 12:11 AM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  3. what is causing this seemingly simple app to crash?
    By Shadow12345 in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 08:36 PM
  4. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 02:22 PM
  5. allegro causing a crash
    By kooma in forum Game Programming
    Replies: 5
    Last Post: 04-06-2002, 02:01 PM

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