I am trying to tokenize a file and save the tokens into an array within a function named tokenize. Then I would like to take the output of the tokenize function to be used within the main or another function, to do something.
The problem is: I'm not quite sure how to move the pointer forward within the main function after tokenizing the file lines. The goal is to keep tokenized lines grouped together and not separated, so the meaning of the words are not lost.
The file.txt would look something like (spaces added between \t for readability):
948213843 \t 644321498 \t 16549816514 \t 13616131216 \t 1646312132 \t 13468486
My question: How would I be able to access the array information being returned from the tokenize function?
Thanks!
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define SIZE 1024
char *tokenize(char *buffer, char *pattern)
{
        int i = 0;
        int j;
        char *arr[15];
        char *token = strtok_r(buffer, "\t", &buffer);
        for (j=0; j < 15; j++)
                arr[j] = malloc(strlen(buffer) * sizeof(char));
        // pattern matching removed to focus only on tokenization
        while (token != NULL)
        {
                strcpy(arr[i], token);
                printf("%s\n", token);
                token = strtok_r(NULL, "\t", &buffer);
                i++;
        }
        // test to verify array data --- good here
        for (i=0; i < 15; i++)
                fprintf(stdout, "test: %s\n", arr[i]);
        return *arr;
}
int main(int argc, char *argv[])
{
        FILE            *filename;
        static char     buffer[SIZE];
        filename = fopen("file_name.txt", "rb+");
        if (filename != NULL)
        {
                while (fgets(buffer, SIZE, filename) != NULL)
                {
                        if (buffer[strlen(buffer) - 1] == '\n')
                        {
                                buffer[strlen(buffer) - 1] = '\0';
                                // the matching search pattern will grab the line of data to be tokenized
                                char *token = tokenize(buffer, "948213843");
                                // test print -- not good here
                                if (token != NULL)
                                {
                                        for (int i=1; i < 15; i++)
                                                fprintf(stdout, "sucks: %s\n", token);
                                }
                                // do something with the tokens
                                // doSomethingWithToken(token);
                        }
                }
        }
}
 
     
    