Home > Software design >  Removing extra line at the end of a file parsed into a double dimensional array of characters (char
Removing extra line at the end of a file parsed into a double dimensional array of characters (char

Time:01-24

I am currently developing a function in C that parses a file into a double dimensional array of characters (char **), the problem is that I get an extra line at the end, and I don't see how to fix that.

Can you help me?

Ps: My school requires me to use getline() and fopen().

Here is the code:

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

void my_free_word_array(char **word_array)
{
    size_t i = 0;
    if (!word_array) {
        return;
    }
    while (word_array[i] != NULL) {
        free(word_array[i]);
          i;
    }
    free(word_array);
}

ssize_t my_put_str_arr(char **arr, int fd)
{
    char cr = '\n';
    ssize_t count = 0;
    size_t i = 0;
    if (!arr)
        return -1;
    while (arr[i]) {
        count  = write(fd, arr[i], strlen(arr[i]));
        count  = write(fd, &cr, 1);
        i  ;
    }
    return count;
}

void append_word_array(char ***array, char *line)
{
    size_t array_len = 0;
    while ((*array)[array_len] != NULL) {
        array_len  ;
    }
    size_t len = strlen(line);
    if (line[len - 1] == '\n') {
        line[len - 1] = '\0';
    }
    (*array)[array_len] = strdup(line);
    (*array) = realloc((*array), (array_len   2) * sizeof(char *));
    (*array)[array_len   1] = NULL;
}

void fill_from_file(char ***array, FILE *file)
{
    char *line_buff = NULL;
    size_t line_buff_size = 0;
    ssize_t line_size = getline(&line_buff, &line_buff_size, file);
    while (line_size >= 0) {
        append_word_array(array, line_buff);
        free(line_buff);
        line_buff = NULL;
        line_size = getline(&line_buff, &line_buff_size, file);
    }
    free(line_buff);
}

char **my_load_file_to_word_array(const char *filepath)
{
    char **word_array = NULL;
    FILE *file = fopen(filepath, "r");
    if (!file) {
        return NULL;
    }
    word_array = malloc(sizeof(char *));
    if (!word_array) {
        return NULL;
    }
    word_array[0] = NULL;
    fill_from_file(&word_array, file);
    fclose(file);
    return word_array;
}

int main (int argc, char **argv)
{
    char **file = my_load_file_to_word_array(argv[1]);
    my_put_str_arr(file, 1);
    my_free_word_array(file);
    return 0;
}

Here is the content of the tested file (I added the \n \0 to make it easier for you to see):

My name is Saul.\n
I am Saul Goodman.\n
Better call Saul.\0

And this is the result I get :

My name is Saul.
I am Saul Goodman. 
Better call Saul.

CodePudding user response:

The "problem" with your code is that the function my_put_str_arr() prints the stored lines eached followed by a single \n character. If you don't want to print the last \n you would need to test if a next line exists. You could change your loop as follows:

while (arr[i]) {
    count  = write(fd, arr[i], strlen(arr[i]));
    i  ;
    if (arr[i]) {
        count  = write(fd, &cr, 1);
    }
}
  • Related