0

I am trying to implement three functions for a program:

  • The first one counts the number of lines in a file,
  • The second one creates a dynamic array and fills it with the content of the file - one row for each row of the table, and returns a structure with two fields: the first one stores a pointer to the created dynamic array, and the second one stores the size of that array.
  • The third function prints this array.

I need help with the code I've written so far:

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

struct vehicle {
    char brand[40];
    char model[40];
    int production_year;
    float engine_capacity;
};

struct array {
    char*** ptr;
    int size;
};

int count_lines(FILE* file) {
    int temp;
    char temp1[50];
    char temp2[50];
    int temp3;
    int line_count = 0;
    while (!feof(file)) {
        fscanf(file, "%s", &temp1);
        fscanf(file, "%s", &temp2);
        fscanf(file, "%d", &temp);
        fscanf(file, "%d", &temp3);
        line_count++;
    }
    rewind(file);
    return line_count;
}

struct array read_array(FILE* file) {
    struct array arr;
    int n;
    n = count_lines(file);
    char** t1 = malloc(n * sizeof(char*));
    for (int i = 0; i < n; i++)
        t1[i] = malloc(50 * sizeof(char));

    int i = 0;
    while (i < n) {
        fgets(t1[i], 50, file);
        i++;
    }

    arr.ptr = &t1;
    arr.size = n;

    return arr;
}

void print_array(struct array arr) {
    printf("Array t1:\n");
    for (int i = 0; i < arr.size; i++) {
        printf("%s", t1[1]);
    }
}

int main() {
    FILE* stream_read;
    struct vehicle cars;
    struct array my_array;
    if ((stream_read = fopen("dane.txt", "r")) == NULL)
        printf("File 'data' could not be opened\n");
    else
        printf("File 'data' has been opened\n");
    int line_count;

    line_count = count_lines(stream_read);

    my_array = read_array(stream_read);

    print_array(my_array);

    if (fclose(stream_read)) {
        printf("File 'data' was not closed\n");
    }

    getchar();
    return 0;
}

I'm having trouble with printing the array, as I'm doing something wrong when passing the array from the function to main and printing it. Could you please help me fix and understand it

I'm sure that the text is properly being stored in the array, but I have no idea how to pass it to other functions so that it doesn't produce random symbols.

einpoklum
  • 118,144
  • 57
  • 340
  • 684

1 Answers1

1

You have implemented this part of the requirements incorrectly:

two fields: the first one stores a pointer to the created dynamic array [...]

The dynamic array is the allocated space, not the (local to function read_array) variable in which you initially store a pointer to that space. The local variable exists only while the function is executing. After it returns, any pointers to its local variables, such as the one you return inside your structure, become indeterminate.

You want this:

struct array {
    char **ptr;
    int size;
};

and this:

    arr.ptr = t1;  // assign the value of t1, not a pointer to t1
    arr.size = n;

    return arr;

And in function print_array(), you need to use the pointer received in the structure to access the lines. There is no t1 in that function, but after the above changes, arr.ptr should work fine in its stead.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157