I am trying to code a simple program that does the following:
- Selects a file
- Selects what to do with the file (fopen modes: r, w, a, r+...)
- Writes to the file
this is my code so far:
//Gets future file content
printf("Write content:\n");
char content[100];
fgets(content, 100, stdin);
//Selects file
printf("Select output file: ");
char file[30];
fgets(file, 30, stdin);
//Selects mode
printf("Select mode: ");
char mode[3];
fgets(mode, 3, stdin);
FILE *fp;
fp = fopen(file, mode);
if (fp == 0) {
printf("File NOT opened\n");
}
I want the variable "file" to be used as the string/path to the file, and same goes for the "mode" variable. When I run the program I get that the file is not opened, meaning that fp is a null pointer.
PS: It is not the whole code, but that's what's breaking it
Thanks in advance