/* * filew4.c */ #include int getans(void); int main(void) { FILE * fp; // declare file pointer:fp char first[15], last[15], phone[20], email[30]; // declare char arrays char addressbook[10]; // declare char array printf("Enter the name of a new address book: "); // prompt for input scanf("%s", addressbook); // read from stdin // interpret as string // assign to elements of array addressbook if( (fp=fopen(addressbook,"w")) == NULL ) // open file file for writing // assign file handle address to fp // if address is NULL, do block { printf("Can't open file %s.\n", addressbook); // print error return 1; // return 1 to calling program } do { printf("First Name: "); scanf(" %s", first); printf("Last Name: "); scanf(" %s", last); printf("Phone Number: "); scanf(" %s", phone); printf("Email Address: "); scanf(" %s", email); getchar(); // clear input buffer fprintf(fp, " %s %s", first, last); fprintf(fp, " %s %s\n", phone, email); printf("Do you wish to enter another person's information? (y/n): "); } while( getans() ); fclose(fp); printf("The file %s written and closed.\n", addressbook); 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 } }