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("es[minValue], "es[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;
}
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