Separate concerns of file reading/writing and parsing
Closed this issue · 1 comments
thierryseegers commented
The function removeCode
has multiple responsibilities:
- Confirm the input
FILE
exists and can be read. - Create the output
FILE
for writing. - read/parse/write work.
I suggest modifying removeCode
to accept FILE*
instead and move the responsibility of file checking/creating outside of it. I think this is will facilitate unit testing.
bouzinabdotcom commented
Created FILE * open_iofile(char*, int) to fix it.
FILE * open_iofile(char * filename, int io) {
/**
* Function that opens input or output file depending on io parameter
* io == 0 : reading input file
* io == 1 : writing to output file
* if file can't be opened: stops execution
* else it returns a pointer on it
* */
FILE *f = NULL; //pointer to filename
if(f = fopen(filename, (io ? "w" : "r"))) //if filename is openned for reading/writing succesfully
return f; //return pointer
else { //error handling for opening for reading/writing problems (ex: permissions)
printf("Error: couldn\'t open file for %s.\n", (io ? "writing" : "reading"));
printf("file: %s\n", filename);
exit(EXIT_FAILURE);
}
}
Called it in the main function as follows:
removeCode(open_iofile(filename, 0), open_iofile(newfilename, 1));