Home > Blockchain >  C function to load file content in string array
C function to load file content in string array

Time:03-03

What I'm exactly trying to do is: properly initiate a 2D char array(array of strings), load the contents of the file to the array breaking each '\n' in another function.

Since we cannot return double arrays from functions in C, at the moment I'm trying to use strncpy() reading the file line by line. I tried lots of variants but I kept messing with the memory, so I faced a lot of segfaults and bus errors.

Here is a modal demonstrating how simple what I'm trying to do is:

int main()
{
  char *file_content[];
  load_file_to_array(file_content);
}

void load_file_to_array(char *to_load[]){
  // something to update file_content
}

EDIT: I'm not asking anyone to write my code for me. I'm frustrated with researching about this specific topic, so I decided to ask about what you guys think. Any different approach or the methodology itself are appreciated.

I have a good hunch about my approach that it's completely off the track.

I've looked at a lot of posts and articles about dynamic memory. The closest thing that I found to what I'm trying to accomplish was this post

EDIT[2]:

As @pm100 explained in the comments I tried:

 #include <stdio.h>
    #include <stdlib.h>
    #define MAX_SIZE 255
    
    void load_array(char *, char *[]);
    
    int main(){
    
        // NULL terminate array
        char *fn = "text.txt";
        char *arr[MAX_SIZE] = { NULL };
        // Also tried: 
        // char **arr = malloc(sizeof * char*MAX_SIZE)
        // arr[0] = malloc(sizeof(char)*MAX_SIZE);
        load_array(fn, arr);
    }
    
    void load_array(char *fn, char *fcontent[]){
    
        FILE * file = fopen(fn, "r");
    
        char line[MAX_SIZE];
        
        int i = 0;
    
        // read file line by line with fgets()
        while (fgets(line, sizeof(line), file)) 
        {
            // this part I'm not sure of. maybe setting with         fgets makes more sense.
            strcpy(fcontent[i], line);
            i  ;
            // realloc()
            fcontent = realloc(fcontent, sizeof (char*) * (i   1));
            fcontent[i] = malloc(sizeof(char) * MAX_SIZE);
            // null terminate the last line
            *fcontent[i] = 0; 
        }
    
        fclose(file);
    }

I get a segfault after running the program.

CodePudding user response:

Apart from saying this is far from an efficient solution, here's yours slightly modified:

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


#define MAX_SIZE 1024
char** load_array(const char *fn) {

    char** fcontent = (char**)malloc(0);

    FILE * file = fopen(fn, "r");

    char line[MAX_SIZE];

    int i = 0;

    // read file line by line with fgets()
    while (fgets(line, sizeof(line), file))
    {
        fcontent = (char**)realloc(fcontent, (i   1) * sizeof(char*));
        fcontent[i] = (char*)malloc(strlen(line)   1);
        // this part I'm not sure of. maybe setting with         fgets makes more sense.
        strcpy(fcontent[i], line);
        i  ;
    }

    fcontent = (char**)realloc(fcontent, (i   1) * sizeof(char*));
    fcontent[i] = NULL;

    fclose(file);
    return fcontent;
}

int main()
{
    char **lines = load_array("test.txt");
    int i = 0;
    while (lines[i])
        printf("%s", lines[i  ]);

    i = 0;
    while (lines[i])
        free(lines[i  ]);
    free(lines);

    return 0;
}
  • Related