/* * filew5.c */ #include int getans(void); int main(void) { FILE * fp; // declare file pointer:fp char payee[36], desc[51], month[16], year[5]; // declare char arrays int checkno, date; float amount; char accountname[15]; printf("Enter the name of checking account record: "); // prompt for input scanf("%s", accountname); // read from stdin // interpret as string // assign to elements of array addressbook getchar(); // clear input buffer if( (fp=fopen(accountname,"a")) == NULL ) // open file file for append // assign file handle address to fp // if address is NULL, do block { printf("Can't open file %s.\n", accountname); // print error return 1; // return 1 to calling program } do // do block , then run evalueation after first run { printf("Payee Name: "); // prompt for input fgets(payee,36,stdin); // read max of 36 chars from stdin, // assign to array payee printf("Check Number: "); // prompt for input scanf(" %s", &checkno); // read string, assign to int checkno getchar(); // clear input buffer printf("Month: "); // prompt for input scanf(" %s", month); // read string, assign to arrya month getchar(); // clear input buffer printf("Day of Month: "); // prompt for input scanf(" %d", &date); // read digit, assign to int date getchar(); // clear input buffer printf("Year: "); // prompt for input scanf(" %s", year); // read string, assign to array year getchar(); // clear input buffer printf("Amount of the check: "); // prompt for input scanf(" %d", &amount ); // read digit, assign to int amount getchar(); // clear input buffer // printing to file handle pointed to by fp fprintf( fp, "\nPayee Name: %s", payee); // fprintf( fp, "Date: %s %d, %s\n", month, date, year); // fprintf( fp, "Check Number: %d\n", checkno); // fprintf( fp, "Amount: %.2f\n", amount ); // fprintf( fp, "Description: %s\n", desc); // printf("Do you wish to add information for another check? (y/n): "); } while( getans() ); fclose(fp); printf("The file %s written and closed.\n", accountname); return 0; } //// Functions int getans(void) // header: takes nothing, returns int { char cc; if( (cc=getchar()) == 'y' ) // read from stdin, assign to cc // if cc == 'y', do block { getchar(); // clear input buffer return 1; // they want to enter more people } else { return 0; // they don't want to enter more people } }