I'm trying to create/open a file using c. I enter the name using the fgets command, so I can not overflow the buffer, like this:
void main() {
printf("Enter file name: ");
char * fileName = (char *) calloc(MAX_BUFFER_SIZE, sizeof(char));
fgets(fileName, MAX_BUFFER_SIZE, stdin);
FILE *inputFile = fopen(fileName, "w");
if(inputFile==NULL) perror(fileName);
}
Using the debugger, I can see that the value I entered for the name of the file, is the one I wish, but the fopen function returns NULL pointer and I get the "Invalid argument" error. If I use scanf("%s", fileName)
instead there is no problem and the file is created but in this way I could overflow the buffer. Any ideas why the first example is not working?
Thanks in advance :)