0

in the text file it'll say something like this:

12 4 23 76

7 3 12 54

1 54 2 67

...

int arr[26];
int arr2[26];
int arr3[26];

int main(){
fp = fopen ("myfile.txt", "r");
  while (fgets(store, sizeof(store), fp)){

    //I tried using scanf but I couldn't get it to work

   printf("%s", store); //prints out a line
  }

}

I know that the 'store' has the string that I want to work with. how can I grab the integers from 'store' that are separated by space and put them into an array? so I would want

arr[0]=12 arr[1]=4 arr[2]=23 arr[3]=76,

arr2[0]=7 arr2[1]=3 arr2[2]=12 arr2[2]=65,

arr3[0]=1 arr3[1]=54 arr3[2]=2 arr3[3]=67

Juho Jung
  • 19
  • 3

2 Answers2

0

you can use strtok() and atoi() function if string format is guaranteed.

here's draft of code.

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

int arr[3][26];
char store[100];
char *split;
int i, j;
int main(){
    i = 0;
    FILE* fp = fopen ("myfile.txt", "r");
    while (fgets(store, sizeof(store), fp)){
        printf("%s", store); //prints out a line
        split = strtok(store, " ");
        j = 0;
        while(split) {
            arr[i][j++] = atoi(split);
            split = strtok(NULL, " ");
        }
        i++;
    }

    for(i = 0; i < 3; i++) {
        for(j = 0; j < 4; j++) {
            printf("%2d ", arr[i][j]);
        }
        puts("");
    }
}

I hope that you can utilize this code for what you want.

-> I fixed code which is able to run.

you can modify this code because I tested and it worked.

I'm not sure which point you struggled, but I suggest you to check out how to use those functions in web.

  • I tried it, however when I do printf("%d\n", arr[I][j]); after the split = streak(NULL, " ");, it only prints out 0s – Juho Jung Oct 14 '19 at 19:14
  • https://stackoverflow.com/questions/26191011/c-fgets-strtok-and-atoi-to-read-a-line-in-c here's similar code! I hope it helps you :) – ChangHo Seo Oct 14 '19 at 19:29
0

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);
}
Lily AB
  • 392
  • 2
  • 6