/* Course Number : ISYS 116 Student Name : Jason Gradziel Student Number : 826-135-535 Professors Name : Gene Rychlewski Class Name : Thursday Lab Title : File Input/Output - Write to File */ #include #include #include FILE *Safe_fopen(char *, char *); void Safe_fclose(FILE *); typedef struct { char id_no[3]; int q_o_h; double price; } PART_REC; main() { clrscr(); FILE *input_file, *output_file; PART_REC part; char input_file_name[51], output_file_name[51]; int record_count = 0; printf("\nEnter the name of the text file: "); gets(input_file_name); input_file = Safe_fopen(input_file_name, "r"); printf("\nThe file was succfully opened."); printf("\nEnter the name of the sequential file: "); gets(output_file_name); output_file = Safe_fopen(output_file_name, "wb"); while ( fscanf(input_file, "%s %d %lf", part.id_no, &part.q_o_h, &part.price) == 3) { fwrite(&part, sizeof(PART_REC), 1, output_file); ++record_count; } printf("\n\n%d records were written.", record_count); Safe_fclose(input_file); //printf("\nThe file was closed succfully."); Safe_fclose(output_file); printf("\nThe file was closed succfully."); getch(); return 0; } FILE * Safe_fopen(char *f_name, char *mode) { FILE *file_ptr; if ( (file_ptr = fopen(f_name, mode)) == NULL) { fprintf(stderr, "\nError: File %s cannot be opened.", f_name); getch(); exit(1); } return file_ptr; } void Safe_fclose(FILE *file_ptr) { if ( fclose(file_ptr) == EOF) { fprintf(stderr, "\nError: The file cannot be closed."); getch(); exit(1); } return; }