user3659222
user3659222

Reputation:

How to insert array into array in C

What can be done in order to insert arrays into another array. I do happen to have a file which I'm looping through so my goal by now is to take each line within the file and put it into an array (An array of lines). Each line has some data related to a student and his/her tests results, for instance:

Mark Zuckerberg 10.0 5.5 9.7

Bill Gates 10.0 1.5 6.7

Example code:

FILE* file;
char buffer[256];
char arrayOfLines[500];
char line[256];   

file= fopen("I:\\students.txt","r+");

/* Helps to iterate through each line */
while (fgets(buffer, sizeof(buffer), file)) {
      //some logic to-be-coded
      line = buffer; // just an idea does not work
      arrayOfLines[i] = line; // just an idea does not work
}

Expected result:

arrayOfLines[] = {" Mark Zuckerberg 10.0 5.5 9.7", "Bill Gates 10.0 1.5 6.7" };

I certainly know this is somehow easy to achieve but after working with java for a long time it turned out to be a little bit tricky, could someone please help me out with this matter? Everything works just fine when it comes to iterating and reading. However, when it comes to "playing" with each line it gets creepy. Perhaps I need to change my approach, any help would be welcomed.

Upvotes: 0

Views: 3284

Answers (2)

BLUEPIXY
BLUEPIXY

Reputation: 40145

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

int main( void ){
    FILE *file = fopen("I:\\students.txt","r");
    char buffer[256*500];
    char *arrayOfLines[500];
    char *line = buffer;
    size_t buf_siz = sizeof(buffer);
    int i=0, n;

    while (fgets(line, buf_siz, file)) {
        char *p = strchr(line, '\n');
        if(p)
            *p = '\0';//remove newline
        else
            p = strchr(line, '\0');
        arrayOfLines[i++] = line;
        buf_siz -= p - line + 1;
        if(p + 1 == buffer + sizeof(buffer))
            break;//buffer full!
        line = p + 1;
    }
    fclose(file);
    n = i;
    for(i=0; i<n; ++i)
        puts(arrayOfLines[i]);
    return 0;
}

Upvotes: 0

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76413

Array names decay into pointers when you try to assign them. In order to copy the contents of buffer to another char[] variable, you'll have to use memcpy, strcpy or strncpy

Apart from that, you do have an issue with your arrayOfLines variable. It's not an array of lines. It's an array of 500 characters, so it's not even big enough to hold 2 lines, what you need is:

char array_of_lines[500][256];

And to copy the data, I'd use:

memcpy(aray_of_lines[i], buffer, sizeof buffer);//where `i` is the current line, starting at 0

It might be worth looking into malloc-ing the array of lines here, because this code requires 128,000 bytes of stack space to be available, just to accommodate the array_of_lines array, so I'd write:

char *array_of_lines[500];
//in the loop:
array_of_lines[i] = malloc(strlen(buffer) + 1);
//check for NULL, and all that
strncpy(array_of_lines[i], buffer, strlen(buffer));

Upvotes: 1

Related Questions