Feedback from Discord Together C & C - Reslashd/simple_encrypt GitHub Wiki

a_rui_r:

char file_content[MAX_LINES][MAX_LENGTH] = {{'\0'}};
@
if ((file_ptr = openFile(file_name, READ_MODE)) != NULL) {
printf("\n*** FILE OPENED ***\n");

// Read file and store in file_content
lines_read = readFile(file_content, file_ptr);
closeFile(file_ptr);

// Set signature flag for showMenu
has_signature = checkSignature(file_content, lines_read);
showMenu(has_signature, file_content, lines_read, file_ptr, file_name);
} else {
printf("\n*** ERROR OPENING FILE ***\n");
}
@
FILE* openFile(char* file_name, char* file_mode) {
return fopen(file_name, file_mode);
}
@
int readFile(char file_content[][MAX_LENGTH], FILE* file_ptr) {
int lines_read = 0;

// While the end of the file is not reached yet AND there are no errors
// reading the file
while (!feof(file_ptr) && !ferror(file_ptr)) {
if (lines_read >= MAX_LINES) {
printf("\nMaximal line limit reached, stop reading");
return lines_read;
}
if (fgets(file_content[lines_read], MAX_LENGTH, file_ptr) != NULL) {
lines_read++;
}
}
return lines_read;
}

Between each @ is where I changed the code.

MyNameIsTrez

Make sure to use your FILENAME_LENGTH in fgets(file_name, 100, stdin);

Because I don't know how you compile it, I can recommend my favorites that help a ton with finding subtle issues:
-Wall -Wextra -Werror -Wpedantic -fsanitize=address,undefined -Wfatal-errors -g