I have a program that is creating multiple files. There is a function for each file being created. Within each function is the exact same code to create the file name, open/create the file for writing, set its permissions and at the end close the file. I decided to make a function for opening the file and closing the file so I can just call that instead of using the same code each time. The code previously looked like the following in each function:
void WriteFile1(char *name) {
FILE *file;
char *filename; //This is being malloc'ed because it initially consisted of multiple strings
if (!(filename = malloc(sizeof(char *) * (strlen(name) + 1)))) MallocError();
if (!(file = fopen(filename, "w"))) {
fprintf(stderr, "Unable to open %s. Exiting \n", filename);
exit(1);
}
fchmod(fileno(file), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH);
//a bunch of fprintf statements here
if (fclose(file)) {
fprintf(stderr, "Error closing %s. Exiting...\n", filename);
exit(1);
}
}
This worked perfectly fine. I had no issues. Now it looks like the following:
void WriteFile1() {
FILE *file;
OpenFile(file, "filename.asdf");
//fprintf statements
CloseFile(file, "filename.asdf");
}
void OpenFile(FILE *file, char *name) {
if (!(file = fopen(name, "w"))) {
fprintf(stderr, "Unable to open %s. Exiting... \n", name);
exit(1);
}
fchmod(fileno(file), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH);
}
void CloseFile(FILE *file, char *name) {
if (fclose(file)) {
fprintf(stderr, "Error closing %s. Exiting...\n", name);
exit(1);
}
}
It seg faults as soon as I get to the first fprintf statement in WriteFile1(). Am I doing something incorrectly with the FILE variable? It seems like it should work just like it did before. The only difference is the malloc of the filename string, which I am instead passing as name and giving the actual value in quotes.
Thank you
This piece of code is wrong:
Here you are just assigning to the local
filevariable.You have to return the
file, so your WriteFile1() function can work with that FILE*