how to use strtok()?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slizorn
    New Member
    • Jul 2008
    • 34

    how to use strtok()?

    the thing is.. i need to read a text file
    [code=cpp]
    <matrix>
    rows = 2
    cols = 2

    1 2
    2 4
    </matrix>
    [/code]
    and i need to store the data in a double array [10][10]..
    where the matrix max size is 10 by 10...
    also i nid to extract the values of rows and cols and save it to row and col variables..
    i got a suggestion to use strtok()..
    but i am not sure how to do so...
    could someone tell me how in relation my problem above?
    thanks in advance
  • kreuzen
    New Member
    • Aug 2008
    • 2

    #2
    Strtok() basically tokenizes your string (string tokenizer function). You input a string and specify the delimiters to where it should be split.

    So for example, if you have

    char string[1024] = "I,know,how,to, use,strtok"; (delimiters are the commas)
    char delim[512] = ",';

    Output:
    I
    know
    how
    to
    use
    strtok

    then you put strtok(string, delim). You can put multiple delimiters so you're not limited to one. Example: char delims[512] = ",-@" . This will cut the string when it sees a comma, a dash, or the "at" sign.



    Hope that helped.

    Comment

    • slizorn
      New Member
      • Jul 2008
      • 34

      #3
      Originally posted by kreuzen
      Strtok() basically tokenizes your string (string tokenizer function). You input a string and specify the delimiters to where it should be split.

      So for example, if you have

      char string[1024] = "I,know,how,to, use,strtok"; (delimiters are the commas)
      char delim[512] = ",';

      Output:
      I
      know
      how
      to
      use
      strtok

      then you put strtok(string, delim). You can put multiple delimiters so you're not limited to one. Example: char delims[512] = ",-@" . This will cut the string when it sees a comma, a dash, or the "at" sign.



      Hope that helped.

      ah i see what u mean...
      but how am i to get it to skip lines and read and store the values?
      i can do the storing values into the matrix but how to do it..when i need the data on the right hand side of the line col = 2; ????

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        If I were reading doubles into a 10x10 array, I would read 100 doubles as though I had 100 double array.

        That is, the memory layout of:
        double arr[10][10];

        and

        double arr[100];

        is the same.

        Read this: http://bytes.com/forum/thread772412.html. Look at the last example.

        Comment

        Working...