the code posted by ChangHo is pretty accurate barring a few corrections.
Since the data contained in the text file, seems to contain blank lines, the primary fix is to ensure that these lines are ignored.
I notice in your comment, that when you try printing the vales after "strtok", you notice 0s.That's because the value of the variable "j" has been incremented....And is yet to be assigned a new value.....
arr[i][j++] = atoi(split);// j has been incremented
split = strtok(NULL, " ");
printf("%d\n", arr[i][j]);//displays 0,since "j" is yet to be assigned a value
All in all, here is the updated version of the code posted....
Please see the comments......
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int arr[3][26];
char store[100];
int i = 0;
int j = 0;
char *split;
int main(){
FILE *fp;
fp = fopen ("test.txt", "r");
while (fgets(store, sizeof(store), fp)){
if(strlen(store) == 1)//When fgets returns just the newline character
continue; //skip the line
printf("%s", store); //prints a line
split = strtok(store," ");
j = 0;
while(split != NULL){
arr[i][j] = atoi(split);
split = strtok(NULL," ");
printf("%d\n", arr[i][j]);//print value in array before j is incremented
j++;
}
arr[i][j] = '\0';//terminate the sub array with the NULL character
i++;
}
//print the result
for(int i = 0;i<3;++i){
for(int j = 0;arr[i][j] != '\0';++j)
printf("%d ",arr[i][j]);
putchar('\n');
}
fclose(fp);
}