Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Question on STRTOK...

Options
  • 09-09-2005 3:29pm
    #1
    Registered Users Posts: 1,590 ✭✭✭


    Hi,

    I'm havin a problem with a part of my c program. I'm using strtok and want to take this info from a text file:

    a 0.1 0.2 0.3
    b 1.1 1.2 1.3
    c 3.1 3.2 3.3

    it's taking it all in no problem and the numbers are stayin fine but the letters keep being over written so that when i print it out all i see is

    c 0.1 0.2 0.3
    c 1.1 1.2 1.3
    c 3.1 3.2 3.3


    This is my code:

    while(fgets(line, 1024, mixfile))

    {

    printf("%s", line);



    if (line[0]!='#')

    {

    name[filecount] = strtok(line, " \t\r\n"); // Why overwriting the names?

    start[filecount]= atof(strtok(NULL, " \t\r\n"));

    gain[filecount] = atof(strtok(NULL, " \t\r\n"));

    pan[filecount] = atof(strtok(NULL, " \t\r\n"));

    printf("\n%s\n", name[filecount]);

    filecount++;

    }

    }




    for (i=0; i<filecount; i++)

    {

    printf("\n%d: %s %.1f %.1f %.1f\n", i, name, start, gain, pan);

    }

    I don't know whats wrong. Any clues?

    Atilla


Comments

  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    You're populating name[] with pointers to 'line'. Line keeps changing. You should be doing something like strcpy(name, strtok(line....). Obviously, you must allocate memory appropriately.


Advertisement