-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.

1
  • Study strcat. Commented Mar 4, 2020 at 13:42

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I try but it just does not work

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.