0

I have to write a program that sorts quotes by length in a text file and outputs the result to another file. But while the compile works and builds with no error, I still get unexpected results, plus a run error in netbeans:

void read_in(char** quotes, size_t* size){
*size = 0;
FILE *filePointer;
filePointer = fopen("quotes.txt", "r");

if (filePointer == NULL){
    perror("Error: File cannot be opened! \n");
    exit(1);
}

char tempArr[MAX_LEN];

while(fgets(tempArr, sizeof(tempArr), filePointer)){
    if(*size == MAX_QUOTES){
        printf("Warning: File contains more than 7 quotes. Load halted. \n");
        return;
    }

    char* ptrMem = (char*)malloc(sizeof(char) * strlen(tempArr));

    if(!ptrMem){
        printf("Error: Memory could not be allocated! \n");
        return;
    }

    strcpy(ptrMem, tempArr);
    ptrMem[strcspn(ptrMem, "\n")] = '\r';
    quotes[(*size)++] = ptrMem;

}  
fclose(filePointer);
}

//-----------------------------------------------------------------------------------------------

void selection_sort(char **quotes, size_t size){
// Current min value to compare
int minValue;
printf("--- Input:\n");   

// Loop and print quotes from array
for(int i = 0; i < (size - 1); i++){                
    printf("%s\n", quotes[i]);
}

for(int i = 0; i < (size - 1); i++){
    // Determine minimum element
    minValue = i ;
    for(int j = i + 1; j < size; j++) {
        if(strlen(quotes[j]) < strlen(quotes[minValue])){              
            minValue = j;
        }              
        swap(&quotes[minValue], &quotes[i]);      
    }                  
}    
}

//-----------------------------------------------------------------------------------------------

void print_out(char** quotes, size_t size){
    printf("--- Output:\n");
        for (int i = 0; i < size; i++){    
        printf("%s\n", quotes[i]);
    }
}

//-----------------------------------------------------------------------------------------------

void write_out(char** quotes, size_t size){
    FILE *filePointer = fopen("output.txt", "w");

// If file ptr null value, throw exception
    if (filePointer == NULL){
        perror("Error: Cannot write to file!");
        exit(1);
    }

for (int i = 0; i < size; i++){
    fprintf(filePointer, "%s\n", quotes[i]);
}

// Close file ptr after use
fclose(filePointer);
}

//-----------------------------------------------------------------------------------------------

void free_memory(char** quotes, size_t size){
// Loop through array of quotes (pointers) and free each allocation
for (int i = 0; i < size ;i++){
    free(quotes[i]);
}  
}

//-----------------------------------------------------------------------------------------------

void swap(char **quote_A, char **quote_B){
    char *temp = *quote_B;
    *quote_A = *quote_B;
    *quote_A = temp;
}

This is what I get in ouput: enter image description here

For some reason, it's repeating a string (char pointer quote) over and over. What it's supposed to do is have them sorted by length which is what my selection_sort algorithm was supposed to do. All the function prototypes were defined above that code. I figured it would take too much space to put the entire program, but I can if needed.

Also, the main method and header files are defined above.

EDIT: I do get some warning about strcpy not being able to be limited to a max buffer size

  • "For some reason". So what did you do after you found it's not working? What debugging have you done? It is in your best interest to learn to debug effectively. This may be useful: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – kaylum Nov 21 '19 at 03:49
  • The only thing I could figure out was the warning it gave – user3923150 Nov 21 '19 at 03:51
  • You forgot to allocate space for the terminating null character, so you're writing past the end of your allocated string. Add one to the length to fix it. – Tom Karzes Nov 21 '19 at 04:08
  • `(sizeof(char) * strlen(tempArr))` ==> `(strlen(tempArr) + 1)`. [And stop casting `malloc` in C programs.](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). It's neither needed nor advised. – WhozCraig Nov 21 '19 at 06:18

0 Answers0