0

I have a text file I want to upload into a pointer array but I can't find any references besides 2D arrays or languages other than C.

My input.txt:

marbels
fruit
vegetables
marshmellow sprinkle
coffe beans

My source.c (my question is located in the int main(void){})

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

#define MAX_LEN 1000

void sort(size_t size, char* data[]) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (strlen(data[j]) < strlen(data[j + 1])) {
                char* temp = data[j];
                data[j] = data[j + 1];
                data[j + 1] = temp;
            }
        }
    }
}

void printArray(size_t size, char* data[]) {
    for (int i = 0; i < size; i++) {
        printf("%s ", data[i]); //%c -> %s
    }
}
int main(void) {
    
    char* data[1000]; //I want the array to hold maximum 1000 characters

    FILE* file;
    file = fopen("C:\\Users\\EXAMPLE\\desktop\\input.txt", "r");

    if (file == NULL) {
        printf("File Error\n", file);
        return 1;
    }

//between these dashes in my issue with uploading the text from input to the char* data[1000];

    int line = 0;

    //if i build the program with this while-loop, I get an error
    while (!eof(file) && ferror(file)) {
        if (fgets(data[line], MAX_LEN, file) != NULL) { //error C6001: using uninitialized memory 'data'
            line++;
        }
    }

    fclose(file);
//
    size_t size = sizeof data / sizeof data[0];
    sort(size, data);
    printArray(size, data);

    return 0;
}

The error message:

enter image description here

I tried that while loop before and it wasn't great but only thing I could find at the time for this project.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
JohnDoe12
  • 35
  • 5
  • 1
    The variable `data` is an array of pointers, but ***where do these pointers actually point?*** – Some programmer dude Jan 12 '23 at 11:14
  • 1
    Besides that, please make sure you show us a proper [mre]. The code you show won't build. And once the spelling-error is fixed, the loop shouldn't even run with its current condition. – Some programmer dude Jan 12 '23 at 11:15
  • 1
    And please read [Why is “while( !feof(file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – Some programmer dude Jan 12 '23 at 11:16
  • 1
    Also `size_t size = sizeof data / sizeof data[0];` won't tell you how many elements was actually read, only the size of the array which is always `1000` (with the code as shown). Use `line` once you fix the reading loop and the pointer problem. – Some programmer dude Jan 12 '23 at 11:17

1 Answers1

0

As you have mentioned in your question, you will need to use a 2D array or otherwise you can use the malloc() function.

In your code you have a array with 1000 * sizeof(char) bytes. So bassically you have 1000 uninitialised pointers. But there are no pointers allocated that are able to store the lines of the file.

alen
  • 34
  • 3