Thread: Read string line from its end of the line

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Read string line from its end of the line

    Need to read the string line from its end to get the numbers 1245 1295 or tokenize the string and get the numbers 1245 1295

    I am stuck here: Got the strlen ("DM_I_SENSE_RT_MIN_POS_I_SHIFT [INT(16)] 1245 ; 1245"), then do what? Can anyone help me with a sample code?

    <sample string>
    DM_I_SENSE_RT_MIN_POS_I_SHIFT [INT(16)] 1245 ; 1295
    </sample string>

    <code>
    Length=strlen(test_buffer);
    tempValC[i] = (char *)malloc(sizeof(char)*(strlen(test_buffer) + 1));
    tempValC[i] = strtok(test_buffer,";");
    printf("\n %d Value=%s",i,tempValC[i]);
    </code>

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Please learn how to use code tags... [code] ---- [/code]

    Are all the inputs you're likely to handle formatted in the same way?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    <code> is not a valid BB tag. You need to use square brackets [ ] to surround your code tags. And <sample string> is not valid either, even if you use square brackets.

    Some functions to look in to for parsing are strtok (which you're already using) and sscanf. You may want to have strtok split on a space/tab character so that you get the token containing 1245. Alternatively, you can use sscanf:
    Code:
    int first_number, second_number;
    // check if sscanf returns 2, meaning we have two successful conversions
    if (sscanf(test_buffer, "DM_I_SENSE_... [INT(16)] %d ; %d", &first_number, &second_number) == 2)
    Read your documentation for more detail on the scanf functions and play around with some simpler examples first.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Nothing stops you from specifying multiple delimiters in the call to strtok(), as in
    Code:
    strtok(test_buffer, "; ");  /* delimiter is either semicolon or space */
    The string just read is tokenized into 3 parts; so write code to capture the last two.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 08-07-2011, 09:55 PM
  2. Read file line by line and interpret them
    By sombrancelha in forum C Programming
    Replies: 8
    Last Post: 03-17-2011, 09:48 AM
  3. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  4. Read strings line by line until Ctrl+C is pressed
    By r00t in forum C Programming
    Replies: 25
    Last Post: 11-17-2009, 04:18 AM
  5. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 05:41 AM