sscanf to read file stored in string

Discussion in 'C Programming' started by john.chludzinski@gmail.com, Aug 10, 2012.

  1. Guest

    I have an entire file stored in a string. The file is 2 columns of doubles (stored in ASCII, of course). The doubles are separated by an arbitrary number of spaces, maybe tabs too.

    sscanf(file_bytes+offset, "%lf %lf", &x, &y) will return the number of "items" read, in this case 2 doubles.

    I need to know how to increment "offset" to read the next line?

    ---John

    PS> I looked in FAQs but didn't see anything to answer this question.
    , Aug 10, 2012
    #1
    1. Advertising

  2. Fred K Guest

    On Friday, August 10, 2012 11:09:06 AM UTC-7, wrote:
    > I have an entire file stored in a string. The file is 2 columns of doubles (stored in ASCII, of course). The doubles are separated by an arbitrary number of spaces, maybe tabs too.
    >
    >
    >
    > sscanf(file_bytes+offset, "%lf %lf", &x, &y) will return the number of "items" read, in this case 2 doubles.
    >
    >
    >
    > I need to know how to increment "offset" to read the next line?
    >
    >
    >


    Don't use sscanf(). Use strtod() instead.
    Fred K, Aug 10, 2012
    #2
    1. Advertising

  3. writes:
    > I have an entire file stored in a string. The file is 2 columns of
    > doubles (stored in ASCII, of course). The doubles are separated by an
    > arbitrary number of spaces, maybe tabs too.
    >
    > sscanf(file_bytes+offset, "%lf %lf", &x, &y) will return the
    > number of "items" read, in this case 2 doubles.
    >
    > I need to know how to increment "offset" to read the next line?


    I don't think you can do that with sscanf; it doesn't tell you how many
    characters it processed.

    Use strtod() instead. Its endptr parameter can be used to get a pointer
    to the next character after the last one used in the conversion. It
    also has the considerable advantage of being much more robust; if sscanf
    reads something that has the syntax of a floating-point number, but is
    outside the range of the type (such as "1.0e99999999"), the behavior is
    undefined. strtod() can handle such errors.

    Calling strtod() repeatedly will give you one double value at a time,
    delimited by whitespace (which includes both spaces and newlines).

    If you want to detect formatting errors, such as a line that doesn't
    have exactly two numbers, you can scan the string for '\n' and space
    characters and then call strtod().

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Aug 10, 2012
    #3
  4. Ben Pfaff Guest

    Keith Thompson <> writes:

    > writes:
    >> I have an entire file stored in a string. The file is 2 columns of
    >> doubles (stored in ASCII, of course). The doubles are separated by an
    >> arbitrary number of spaces, maybe tabs too.
    >>
    >> sscanf(file_bytes+offset, "%lf %lf", &x, &y) will return the
    >> number of "items" read, in this case 2 doubles.
    >>
    >> I need to know how to increment "offset" to read the next line?

    >
    > I don't think you can do that with sscanf; it doesn't tell you how many
    > characters it processed.


    You can use %n.
    Ben Pfaff, Aug 10, 2012
    #4
  5. James Kuyper Guest

    On 08/10/2012 02:09 PM, wrote:
    > I have an entire file stored in a string. The file is 2 columns of doubles (stored in ASCII, of course). The doubles are separated by an arbitrary number of spaces, maybe tabs too.
    >
    > sscanf(file_bytes+offset, "%lf %lf", &x, &y) will return the number of "items" read, in this case 2 doubles.
    >
    > I need to know how to increment "offset" to read the next line?


    For this kind of work, what you need is strtod(), not sscanf().
    Warning: untested code. As given below, it's uncompilable: it needs a
    context that defines MAX_BYTES, file_bytes, x, and y, and fills in
    file_bytes, and declares a function that the code below can be part of.

    #include <errno.h>
    #include <stdlib.h>
    #include
    const char *nptr = file_bytes;

    for(int i=0; i<MAX_ITEMS && *nptr; i++)
    {
    char *endptr;

    errno = 0;
    x = strtod(nptr, &endptr);
    if(nptr == endptr)
    {
    fprintf(stderr, "x[%d]: \"%s\"\n is empty or does "
    "not have the form expected by strtod()\n", i, nptr);
    break;
    }
    else if(errno == ERANGE && fabs(x) >= HUGE_VAL)
    {
    fprintf(stderr, "x[%d]: strtod(\"%s\") overflowed\n", i, nptr);
    break;
    // ERANGE might or might not also be set on underflow.
    }
    else if(*endptr == '\0')
    {
    fprintf(stderr, "file ends without a y value for item %d\n", i);
    break;
    }
    nptr = endptr;

    errno = 0;
    y = strtod(nptr, &endptr);
    // Insert similar error handling code here.
    nptr = endptr;
    }

    The error handling here is just basic, it can be improved. In
    particular, you should probably limit the number of bytes that are
    printed from nptr: search for the end of the current line, and don't
    print beyond that. I've left those details to be filled in by the student.
    James Kuyper, Aug 10, 2012
    #5
  6. Eric Sosman Guest

    On 8/10/2012 2:09 PM, wrote:
    > I have an entire file stored in a string. The file is 2 columns of doubles (stored in ASCII, of course). The doubles are separated by an arbitrary number of spaces, maybe tabs too.
    >
    > sscanf(file_bytes+offset, "%lf %lf", &x, &y) will return the number of "items" read, in this case 2 doubles.
    >
    > I need to know how to increment "offset" to read the next line?


    You could use

    int count;
    ...
    sscanf(file_bytes + offset, "%lf%lf%n", &x, &y, &count);
    ... error checking ...
    offset += count;

    Personally, I think it would be easier to use strtod().

    > PS> I looked in FAQs but didn't see anything to answer this question.


    Thanks for checking before posting! Unfortunately, the F
    in FAQ stands for "Frequently," not for "All" ;-)

    --
    Eric Sosman
    d
    Eric Sosman, Aug 10, 2012
    #6
  7. Guest

    wrote:
    > I have an entire file stored in a string. The file is 2 columns of doubles (stored in ASCII, of course). The doubles are separated by an arbitrary number of spaces, maybe tabs too.
    >
    > sscanf(file_bytes+offset, "%lf %lf", &x, &y) will return the number of "items" read, in this case 2 doubles.
    >
    > I need to know how to increment "offset" to read the next line?
    >
    > ---John
    >
    > PS> I looked in FAQs but didn't see anything to answer this question.


    See the "%n" format specifier. You might find it more convenient to use
    strtod() rather than sscanf().
    --
    Larry Jones

    What better way to spend one's freedom than eating chocolate
    cereal and watching cartoons! -- Calvin
    , Aug 10, 2012
    #7
  8. Ben Pfaff <> writes:
    > Keith Thompson <> writes:
    >> writes:
    >>> I have an entire file stored in a string. The file is 2 columns of
    >>> doubles (stored in ASCII, of course). The doubles are separated by an
    >>> arbitrary number of spaces, maybe tabs too.
    >>>
    >>> sscanf(file_bytes+offset, "%lf %lf", &x, &y) will return the
    >>> number of "items" read, in this case 2 doubles.
    >>>
    >>> I need to know how to increment "offset" to read the next line?

    >>
    >> I don't think you can do that with sscanf; it doesn't tell you how many
    >> characters it processed.

    >
    > You can use %n.


    Yes, good point.

    strtod() is still a better tool for this job, though.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Aug 10, 2012
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug
    Replies:
    3
    Views:
    4,353
    Bruce Barker
    Nov 4, 2005
  2. Darrell Grainger

    Re: Unitialized Memory Read In sscanf()

    Darrell Grainger, Jun 24, 2003, in forum: C Programming
    Replies:
    1
    Views:
    811
    Alan Balmer
    Jun 24, 2003
  3. Eric Sosman

    Re: Unitialized Memory Read In sscanf()

    Eric Sosman, Jun 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    837
    Eric Sosman
    Jun 24, 2003
  4. Replies:
    3
    Views:
    410
    Dave Thompson
    Jun 8, 2006
  5. Replies:
    2
    Views:
    622
    Thomas Matthews
    Feb 27, 2007
Loading...

Share This Page