-1

I want to install the data that i read from a csv file into matrix. Here is my code:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define BUFSIZE 1024

int main()
{

    char *filename = "pb0.csv";

    char str[BUFSIZE];
    FILE *fpr;
    fpr = fopen(filename, "r");
    while (fgets(str, BUFSIZE, fpr) != NULL) {
        printf("%s", str);
    }

    fclose(fpr);

    return 0;
}

My output be like:

2; 2; 1; 5; 0; 4
2; 0; 2; 1; 4; 6
2; 2; 1; 4; 6; 5
4; 4; 6; 0; 1; 3
2; 3; 1; 5; 6; 0

I want to write a code which will store all that string of numbers into matrix maybe look like this:

2 2 1 5 0 4
2 0 2 1 4 6
2 2 1 4 6 5
4 4 6 0 1 3
2 3 1 5 6 0

What should i write? Any help will be appreciated.

Schrodinger
  • 39
  • 1
  • 9

1 Answers1

0

Just try this code, it will take comma seperated input and return a matrix

#include <stdio.h>

int main()
{


    FILE *fpr = fopen("pb0.csv", "r");

    int d;
    int counter = 0;
    while(fscanf(fpr, "%d, ", &d) != EOF){
    if(counter < 5){
        printf("%d ", d);
    }else{
        printf("%d\n", d);
        counter = 0; continue;
    }
    counter++;
    }

    fclose(fpr);

    return 0;
}
vishal
  • 249
  • 3
  • 7