Converting the strtok output to integer

This is a discussion on Converting the strtok output to integer within the C Programming forums, part of the General Programming Boards category; Hi! I'm a beginner and I'm trying to figure out how to store the result of strtok() in a Array ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    8

    Question Converting the strtok output to integer

    Hi!

    I'm a beginner and I'm trying to figure out how to store the result of strtok() in a Array of integers...

    If I'm not mistaken, strtok brokens the string into tokens, returning a pointer to this tokens... So, if this pointer points to strings, can I use this function directly as an argument for the function atoi()?

    I've tried to do this, but he results printed are zeros.

    Here follows the relevant section of my code:
    Code:
    while(fgets(line,sizeof line,fread) != NULL)
            {
                temp = strtok(line,delims);
                while (cont <= 40)
                {
                   cont++;
    
                   for (i=ID; i<=Pattern; i++)
                   {
                        if (i==ID)
                        {
                            Node[cont] = atoi(temp);            
                            printf("%d \n", Node[cont]);
                            temp = strtok(NULL, delims);
                        }
                   }
    
    
                }
    Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Add something like
    printf("%s\n", temp);

    or use the debugger to step through the code and examine the variables as you execute each line of code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by gabrielksa View Post
    Hi!

    I'm a beginner and I'm trying to figure out how to store the result of strtok() in a Array of integers...

    If I'm not mistaken, strtok brokens the string into tokens, returning a pointer to this tokens... So, if this pointer points to strings, can I use this function directly as an argument for the function atoi()?

    I've tried to do this, but he results printed are zeros.
    Unfortunately strtok() does more than just provide pointers into a string. It also inserts nulls breaking the main string into a group of smaller strings in the process. Yes, your original string is altered by this process and is thus, useless as a whole after.

    If you want to store the pointers, store them as pointers...
    Code:
    char *tokens[100] = {NULL};
    Then assign the results of strtok() to the array in ascending order.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Btw, there's a standard function called fread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10:42 AM
  3. Converting an integer into a string
    By Signifier in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2007, 11:56 AM
  4. Replies: 5
    Last Post: 06-12-2007, 03:18 PM
  5. How to output tokens in reverse order using strtok
    By Ninz in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2003, 09:00 PM

Tags for this Thread


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