snippetcMinor
C program to read a file containing numbers, and then print ones divisible by 3 to another file
Viewed 0 times
containingfileonesreadnumbersprogramdivisibleprintanotherthen
Problem
The program reads a file containing a list of numbers, and then writes the ones divisible by 3 to another file, and prints the remaining ones (non-divisible) to a third file.
Declaring, opening, and closing the files is alone over 50% of the code, is there anyway to make it more compact?
Declaring, opening, and closing the files is alone over 50% of the code, is there anyway to make it more compact?
#include
#include
int main(void){
FILE *in_file; /*input file*/
FILE *out_file_divis; /*FIle that contains numbers divisible by 3*/
FILE *out_file_remain; /*File that contains the rest of the numbers*/
if((in_file = fopen("total", "r")) == NULL){
perror("total");
exit(EXIT_FAILURE);
}
if((out_file_divis = fopen("divis", "w")) == NULL){
perror("divis");
exit(EXIT_FAILURE);
}
if((out_file_remain = fopen("remain", "w")) == NULL){
perror("remain");
exit(EXIT_FAILURE);
}
int number;
while(fscanf(in_file, "%i", &number) == 1){
if(number%3 == 0){
fprintf(out_file_divis, "%i\n", number);
}
else{
fprintf(out_file_remain, "%i\n", number);
}
}
fclose(in_file);
fclose(out_file_divis);
fclose(out_file_remain);
return 0;
}Solution
The easiest way to make this more compact is to write a function to handle opening a file and just pass in the name and mode you want. Something like this:
Your
Any time you find yourself repeating the same few lines of code, that's a good sign that it should go into a function.
FILE* open_file_or_exit(const char* filename, const char* mode)
{
FILE* fileHandle = NULL;
if((fileHandle = fopen(filename, mode)) == NULL){
perror(filename);
exit(EXIT_FAILURE);
}
return fileHandle;
}Your
main() then becomes:int main(void){
FILE *in_file = open_file_or_exit("total", "r");
FILE *out_file_divis = open_file_or_exit("divis", "w');
FILE *out_file_remain = open_file_or_exit("remain", "w");
// ... rest of main ...
}Any time you find yourself repeating the same few lines of code, that's a good sign that it should go into a function.
Code Snippets
FILE* open_file_or_exit(const char* filename, const char* mode)
{
FILE* fileHandle = NULL;
if((fileHandle = fopen(filename, mode)) == NULL){
perror(filename);
exit(EXIT_FAILURE);
}
return fileHandle;
}int main(void){
FILE *in_file = open_file_or_exit("total", "r");
FILE *out_file_divis = open_file_or_exit("divis", "w');
FILE *out_file_remain = open_file_or_exit("remain", "w");
// ... rest of main ...
}Context
StackExchange Code Review Q#156637, answer score: 5
Revisions (0)
No revisions yet.