Thread: Parse line using strtol

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Parse line using strtol

    Hi I am trying to parse a line of text from a file using strtol, but I can only seem to get the first word on the line. From the M$ help file it says the second paramater is a pointer to wherever the last function stops, and the third is the number base. So why wont this work:
    Code:
    char* endptr;
    in.getline(str, FILTER_NAME_MAX_LINE-2);
    w = (Uint16)strtol(str, &endptr, 10);
    h = (Uint16)strtol(str, &endptr, 10);
    When my text file line looks like:

    Code:
    3 5 \n

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    w = (Uint16)strtol(str, &endptr, 10);
    if (endptr)
       h = (Uint16)strtol(endptr, &endptr, 10);
    should work.

    --
    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.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You start back at the beginning for the second call. The first parameter tells strtol where to start, and it doesn't save any state like strtok, so you have to remember where to continue (endptr tells you this).
    My best code is written with the delete key.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Thanks guys. I dident realize i had to pass the last position into the later calls, but it seems obvious now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Imposing Line Numbers automatically in the C code
    By cavestine in forum C Programming
    Replies: 14
    Last Post: 10-15-2007, 12:41 AM
  2. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  3. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM