#file delimeter

17 messages · Page 1 of 1 (latest)

ashen talon
#

soo, i tryna make a program that have input from file and the file consist of ID#Name#Score and i wanna make it to variable id, variable name and variable score

zenith turretBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

ashen talon
#

i've tried to use strtok but kinda weird in my code

remote sierra
#

;compile

#include <stdio.h>

int main()
{
    int ID, score;
    char name[15] = {0};

    if (sscanf("12#dimwit#100", "%d#%15[^#]#%d", &ID, name, &score) == 3) {
        printf("ID = %d, name = %s, score = %d\n", ID, name, score);
    }
    else {
        perror("Not correctly formatted");
    }
}
daring lagoonBOT
#
Program Output
ID = 12, name = dimwit, score = 100
remote sierra
#

If the formatting string just read %15s then you'd get a greedy match. You'd read "dimwit#100" as the name.
And as a result, you would not be scanning three parameters and the whole scanf() would fail.
Instead, you need to scan the name up until the first delimiter ('#').

ashen talon
#

but this is my code

#

i tried to make the input from file

#

then use strtok to seperate the #, but i don't really understand how can i make it store to my struct cause it's a bit random for me from the strtok, so i use mod 3 since there's only strings from each line

#

then i tried to print it, it so weird

#

that's the outcome if i just use 1 space bar

sterile widget
zenith turretBOT
#
Monke
Please Do Not Send Screenshots!

They're hard to read and prevent copying and pasting.