0

This is my main.c

#include <stdio.h>
#include "fileIO.h"

int main() {
    char array1[MAX_LINES][MAX_CHAR] = {0};
    char array2[MAX_LINES][MAX_CHAR] = {0};
    int occurrences[MAX_LINES];

    readSentences("..//inputFiles/input1.txt", array1);
    readSentences("..//inputFiles/input2.txt", array2);

    insertionSort(array2);
    insertionSort(array1);

    for (int i = 0; i < MAX_LINES; i++) {
        printf("%s", array2[i]);
    }
}

This is the code for reading the .txt file into the array

void readSentences(char inputFilePath[], char inputSentences[][MAX_CHAR]) {
    const char *INPUT_FILE_PATH = inputFilePath;
    FILE *fp = fopen(INPUT_FILE_PATH, "r+");
    int lineNum = 0;
    while (fgets(inputSentences[lineNum], sizeof(inputSentences[lineNum]), fp) != NULL) {
        lineNum++;
    }
    fclose(fp);
}

This is the code I'm using to sort the 2D array of strings

void insertionSort(char inputStrings[][MAX_CHAR]) {

    for (int i = 0; i < MAX_LINES; i++) {
        int j = i;

        while (j > 0 && strcasecmp(inputStrings[j], inputStrings[j-1]) < 0) {
            swap(inputStrings, j, j - 1);
            j--;
        }
    }
}

void swap(char array[MAX_LINES][MAX_CHAR], int index1, int index2) {
    if (index1 != index2) {
        char swap_elem[MAX_CHAR];
        memset(swap_elem, '\0', sizeof(swap_elem));
        strcpy(swap_elem, array[index1]);
        strcpy(array[index1], array[index2]);
        strcpy(array[index2], swap_elem);
    }
}

This is the output I'm getting when printing the string

Australia
Dominican Republic
Ecuador
Hong Kong
Hong Kong
Hong Kong
Hong Kong
Italy
Republic of IrelandTaiwan
Taiwan
Ukraine

Notice how Republic of Ireland and Taiwan are concatenated This is the output when the other array

Australia
Dominican Republic
Ecuador
Hong Kong
Hong Kong
Hong Kong
Hong Kong
Italy
Republic of IrelandTaiwan
Taiwan
Ukraine

Notice how this time its Republic of Ireland and Taiwan which have concatenated

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Matthew
  • 3
  • 2
  • 1
    What is the value of `MAX_CHAR` and how did you input the data? It smells of buffer overflow. – Weather Vane Feb 21 '22 at 09:31
  • @WeatherVane data input from a .txt file using fgets MAX_CHAR = 20 – Matthew Feb 21 '22 at 09:42
  • 4
    And the newline retained by fgets? `strlen("Republic of Ireland")` + 1 (newline) + 1 (terminator) = 21. – Weather Vane Feb 21 '22 at 09:51
  • The give-away in the code now posted is in `printf("%s", array2[i]);` which isn't outputting a newline, therefore it's still in the string. Please see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221). – Weather Vane Feb 21 '22 at 10:49
  • @WeatherVane this didnt seem to fix the issue – Matthew Feb 21 '22 at 11:34
  • You must input the string to a much bigger buffer, remove the newline, check the length, and then copy it. Once it's in the array of strings, it's too late to prevent overflow. That would be like tying a safety rope after you fall off the cliff. – Weather Vane Feb 21 '22 at 11:40
  • @WeatherVane but when I print the string before applying the sort function there is no issue only that the 2d array is not in alphabetical order there is no concatenation – Matthew Feb 21 '22 at 11:44
  • But nothing, just remove the newlines first. You'll never want them as part of the string - they are part of the file. The reason they are retained by `fgets` is so that you can know if it read the whole line, or it was truncated due to buffer size passed to `fgets`. – Weather Vane Feb 21 '22 at 11:47

1 Answers1

0

The problem probably comes from the string Republic of Ireland being too long for the array you store it into.

Republic of Ireland has 19 characters, so storing it into the array requires MAX_CHAR >= 20, but if you read these strings with fgets() from a text file, you should use a temporary array of at least 21 bytes to accommodate the trailing newline and the null terminator before you strip the newline.

chqrlie
  • 131,814
  • 10
  • 121
  • 189