#include #include #include char* names[] = { "book", "pen", "pencil", "eraser" }; int nb_products = 4; int min_stock = 100; // -------------------------------------------------- int compute_id(char* name) { int id = -1; int i; // Bug 1 for ( i = 0; i <= nb_products; i++ ) { if ( strcmp(names[i],name) == 0 ) { id = i; } } return id; } // -------------------------------------------------- int read_one(FILE *ptr) { int max_length = 200; char name[max_length]; float price; char available; int stock; if (fscanf(ptr,"%s %f %c %d\n", name, &price, &available, &stock) < 4) { fprintf(stderr,"Error processing inventory file\n"); exit(1); } int product_id = compute_id(name); if ( stock < min_stock ) { printf("Product id %d has low inventory (only %d items)\n", product_id, stock); } else { printf("Product id %d OK\n",product_id); } return stock; } // -------------------------------------------------- int bug(char *filename) { FILE *ptr; // Bug 3: total_stock should be initialized to zero int total_stock; if ( ( ptr = fopen(filename,"r")) == NULL ) { fprintf(stderr,"File %s could not be opened\n", filename); exit(1); } else { while (!feof(ptr)) { // Bug 2 total_stock = read_one(ptr); } } fclose(ptr); return total_stock; } // -------------------------------------------------- void copy(char *input, char *output) { FILE *in; FILE *out; if ( ( in = fopen(input,"r")) == NULL ) { fprintf(stderr,"File %s could not be opened\n",input); exit(1); } if ( ( out = fopen(output,"w")) == NULL ) { fprintf(stderr,"File %s could not be opened\n",output); exit(1); } // As long as we didn't reach the end of the file while (!feof(in)) { int max_length = 200; char name[max_length]; float price; char available; int stock; if (fscanf(in,"%s %f %c %d\n", name, &price, &available, &stock) == 4) { fprintf(out,"%s \t %7.2f %c %d\n", name, price, available, stock); } } fclose(in); fclose(out); } // -------------------------------------------------- void read1(char *filename) { FILE *ptr; // Open a file for reading and check if operation succeeded if ( ( ptr = fopen(filename,"r")) == NULL ) { fprintf(stderr,"File %s could not be opened\n", filename); exit(1); } else { // As long as we didn't reach the end of the file while (!feof(ptr)) { int max_buffer = 200; char name[max_buffer]; float price; char available; int stock; // Approach 1: we know what to expect, so we can // read the correct types directly if ( fscanf(ptr,"%s %f %c %d\n", name, &price, &available, &stock) == 4 ) { printf("Method1: %s %f %c %d\n", name, price, available, stock); } } } fclose(ptr); } // -------------------------------------------------- void read2(char *filename) { FILE *ptr; // Open a file for reading and check if operation succeeded if ( ( ptr = fopen(filename,"r")) == NULL ) { fprintf(stderr,"File %s could not be opened\n", filename); exit(1); } else { // As long as we didn't reach the end of the file while (!feof(ptr)) { // Approach 2: we can read whole line at once int max_buffer = 200; char line[max_buffer]; if ( fgets(line, max_buffer, ptr) ) { // Getting rid of the newline character at the end of the string line[strlen(line)-1] = '\0'; printf("Method2: %s\n",line); } } } fclose(ptr); } // -------------------------------------------------- int main(int argc, char* argv[]) { if ( argc < 3 ) { fprintf(stderr,"Please enter old and new name of file to copy\n"); return 1; } char *input_file = argv[1]; char *output_file = argv[2]; read1(input_file); printf("\n"); read2(input_file); copy(input_file,output_file); // Uncomment to see the bugs //int stock = bug(input_file); //printf("The total number of items in inventory is %d\n",stock); return 0; }