0

how could I strtok at spaces while reading in data from a buffer? If my text file contained

1 23 50

45 50 30

2 15 30

and I decide to print the array with my code below, it will print line by line. How could I extend this to further divide the array into individual numbers for each index of the array? Eg.

1

23

50, etc...

I've tried playing around with strtok but I keep segfaulting and I wasn't sure where to fix it.

FILE * fp;
char buffer[5000];
int size = 0;
char **entireFile = NULL;
fp = fopen("file.txt","r");

entireFile = malloc(sizeof(buffer) * sizeof(char*));

while (fgets(buffer,5000,fp)!= NULL)
{
    entireFile[size] = malloc(strlen(buffer)+1);
    strcpy(entireFile[size],buffer);         
    size++;
}
FreeStyle4
  • 272
  • 6
  • 17
  • 1
    Why do you need to use strtok? This is one of they very few instances where scanf/fscanf will actually work as expected. Is it a 2d 3x3 array you want? Or 1d Array? – Chirality Mar 07 '16 at 18:55
  • Hmm.. I guess fscanf could work but the only problem is not every line has 3 numbers.. so if I did fscanf("%d %d %d) wouldn't it fail? (The entire input file was larger varying from 1-3 numbers per line) – FreeStyle4 Mar 07 '16 at 18:58
  • 1
    You can scan in one integer at a time with fscanf until the end of the file. In your original example you have a 1d array (buffer). How did you plan on storing a 2d array in a 1d array? If you want to divide the array into individual numbers, scanning them in with fscanf will by far be the easiest way. – Chirality Mar 07 '16 at 19:02
  • I just planned on storing each number at each indivual index. Right now my code stores entireFile[0] = 1 23 50, I want entireFile[0] = 1, entireFile[1] = 23, etc. Ill try fscanf – FreeStyle4 Mar 07 '16 at 19:04
  • Did you manage to resolve your issue? – Chirality Mar 07 '16 at 22:28
  • Yes, I worked around with it and used the fscanf concept. Thank you – FreeStyle4 Mar 07 '16 at 22:36
  • If either mine or @Weather Vane 's answers solved your problem, mark them. If not, submit your own and mark it in 24 hrs. – Chirality Mar 07 '16 at 22:37

2 Answers2

2

I want entireFile[0] = 1, entireFile1 = 23

This can easily be accomplished with fscanf. Here is a pretty good reference for scanning in inputs.

#include <stdio.h>

int main(){

    FILE *in = fopen("in.txt" , "r");
    int hold;

    /* Won't actually store values, but can be used for
       value manipulation inbetween */

    while ( fscanf(in, "%d", &hold) != EOF ){
        printf("Scanned in %d\n", hold);
    }

    fclose(in);

    return 0;

}

If you want this in an array form, add an incrementing integer and change the hold to an array.

#include <stdio.h>

int main(){

    FILE *in = fopen("in.txt" , "r");
    int hold[100], i=0; // Hold a maximum of 100 integers

        while ( fscanf(in, "%d", &hold[i]) != EOF ){
            printf("Scanned in %d\n", hold[i++]);
        }

        fclose(in);

    return 0;

}

You could also do dynamic memory allocation like this:

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

int main(){

    FILE *in = fopen("in.txt", "r");

    int i=0, j;
    char* fragments[2000];
    char entry[100];

    while ( fscanf(in, "%s", &entry[i]) != EOF ){
        fragments[i] = malloc(sizeof(char) * (1 + strlen(entry)));
        strcpy(fragments[i], &entry[i]);
        i++;
    }
    for ( j = 0; j < i; j++ ){
        printf("%s\n", fragments[j]);
    }

    fclose(in);

    return 0;

}
Chirality
  • 745
  • 8
  • 22
0

This will read 1, 2, or 3 numbers on each line of your file. It uses the return value from sscanf to see how many numbers were read from each line. Of course, if there is any rogue non-numerical data, it will mess up.

#include <stdio.h>

int main(void)
{
    FILE *fp;
    int a, b, c;
    int res;
    char str [100];
    if ((fp = fopen("file.txt", "rt")) == NULL)
        return 1;

    while(fgets(str, sizeof str, fp) != NULL) {
        res = sscanf(str, "%d%d%d", &a, &b, &c);
        if (res == 3)
            printf ("3 values %d %d %d\n", a, b, c);
        else if (res == 2)
            printf ("2 values %d %d\n", a, b);
        else if (res == 1)
            printf ("1 value %d\n", a);
        else
            printf ("0 values\n");
    }

    fclose(fp);
    return 0;
}

File content:

1 23 50
45 50 30
2 15 30
10 20

100

Program output:

3 values 1 23 50
3 values 45 50 30
3 values 2 15 30
2 values 10 20
0 values
1 value 100
Weather Vane
  • 33,872
  • 7
  • 36
  • 56