Okay so im currently learning c and my problem is that my teacher wants us to use a code he sent us it works on windows but it doesn't work on Mac, he can't seem to find the problem, im using a MacBook with the M1 chip, the problem on Mac Is when I use Crtl + D to signal EOF after my input in editFileContents it will edit the file but it will keep looping this in the compiler over and over again:
Menu:
1. Display file
2. Edit file
3. Create new file
4. Exit
Choice: Enter the new text (end with Ctrl+D in Unix or Ctrl+Z in Windows):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILENAME_LENGTH 256
#define MAX_TEXT_LENGTH 1024
void deleteFile(char *filename){
if (remove(filename) == 0) {
printf("File %s deleted:\n", filename);
} else {
perror("Could not delete the file");
}
}
void displayFileContents(char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open the file '%s'\n", filename);
return;
}
char line[MAX_TEXT_LENGTH];
while (fgets(line, sizeof(line), file) != NULL) {
printf("%s", line);
}
fclose(file);
}
void editFileContents(char *filename) {
FILE *file = fopen(filename, "a+");
if (file == NULL) {
printf("Could not open the file '%s'\n", filename);
return;
}
char newContents[MAX_TEXT_LENGTH];
printf("Enter the new text (end with Ctrl+D in Unix or Ctrl+Z in Windows):\n");
while (fgets(newContents, sizeof(newContents), stdin) != NULL) {
size_t len = strlen(newContents);
if (len > 0 && newContents[len - 1] == '\n') {
newContents[len - 1] = '\0';
}
fprintf(file, "%s\n", newContents);
}
fclose(file);
}
int createNewFile(char *filename) {
FILE *file = fopen(filename, "r");
if (file != NULL) {
printf("File already exists\n");
fclose(file);
return 0; // out of createFile
}
else {
FILE *file = fopen(filename, "w");
if (file == NULL) {
printf("Could not create the file '%s'\n", filename);
return 1; // out of createFile
}
fclose(file);
}
editFileContents(filename);
return 2;
}
int main() {
char filename[MAX_FILENAME_LENGTH];
printf("Enter the filename to work on\n");
scanf("%s", filename);
int choice;
while (1){
printf("\nMenu:\n");
printf("1. Display file\n");
printf("2. Edit file\n");
printf("3. Create new file\n");
printf("4. Exit\n");
printf("Choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
displayFileContents(filename);
break;
case 2:
editFileContents(filename);
break;
case 3:
createNewFile(filename);
break;
case 4:
printf("Exiting...\n");
return 0; // out of main()
default:
printf("Invalid choice. Try again.\n");
}
}
}
So have been stuck with this problem for a few days I tried everything, that I can think of including chatGPT
I did insure I was editing the right file
I did try my best at troubleshooting the problem but can't seem to find it.
I tried changing from Xcode to the Terminal which didn't change anything.
Your program exits the
whileloop only whenchoiceis 4. But, when the program receives an end-of-file indication, nothing in your program setschoiceto 4.When an end-of-file indication is encountered before
scanfcompletes any conversions of input items,scanf("%d", &choice)returns the value of the macroEOF, and it does not change the value stored inchoice. To detect this, you must test the return value ofscanf. If it is not 1, indicating that one conversion of an input item was completed, thenscanf("%d", choice)did not store a value inchoice.