Hi i am trying to get the values from a comma separated .dat file into an array in C.
The values in the .dat file only consists of 1 and -1.
I have been trying for hours now, so i decided to ask for help.

Here is an example from the .dat file
1,1,1,-1,1,-1,1,-1,-1,-1,1,-1,-1,-1,-1,-1,1,-1,1,-1,1,-1,-1,-1,-1,1,-1,-1,1,1,-1,1,1,-1,1,1,1,1,-1,1,-1,1,-1,1,1,1,1,-1,1,1,1,1,-1,1,1,-1,1,1,1,1,-1,-1

Provided you don't have to deal with a true CSV format with quoting rules, parsing the file is trivial:

#include <stdio.h>

int main(void)
{
    int value;
    
    while (scanf("%d", &value) == 1) {
        printf("%d\n", value);
        
        if (getchar() != ',')
            break;
    }
    
    return 0;
}

Is this what you were having trouble with, or is there some issue with the array? You weren't very specific.

thank you, it works :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.