Basically, I want my code to open a file using a random filename. I expected the code to open the file properly yet, it returns a bad file descriptor even though I'm using a string name.
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main() {
FILE* newFile;
char* newFilename = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[random () % 26];
printf("The file is: %i\n", newFilename);
//Access file
int n = open(newFilename, O_CREAT|O_WRONLY|O_TRUNC, 0777);
newFile = fdopen(n, "wb");
if(newFile == NULL) {
printf("Error: %s\n", strerror(errno));
return 1;
}
free(newFile);
}