/* Little Boy */ /* Fjord Motor Company */ /* David Kucera 52524 */ #include #include #include #include FILE *FilePtr; /*set the file pointer*/ struct Record /*prototypes the Record structure*/ { char Last_Name[20]; char First_Name[20]; char Address[30]; char City[20]; char Province_Code[3]; char Postal_Code[8]; char Telephone[15]; char Year_And_Model[20]; }; int Check_File(void); struct Record Initalize_Array(void); int Open_File(void); int Enter_Record(int Max); int Write_To_File(struct Record File); struct Record Read_From_File(void); struct Record Fill(struct Record Temp); int Print_Record(struct Record Temp); void Display_Long(struct Record Temp); void Display_Short(struct Record Temp); main() { char Choice; char Name_Wanted[20]; int Temp,Cnt=0,Max; struct Record Customer[100]; Open_File(); while(fscanf(FilePtr, "%s", Customer[Cnt].Last_Name) != EOF) { fseek(FilePtr,(1L*(Cnt)*sizeof(struct Record)),SEEK_SET); Customer[Cnt] = Initalize_Array(); Cnt++; Max=Cnt; } do /*Displays Main Menu*/ { printf("\n##### MAIN MENU #####\n\n"); printf("[D]isplay All Names\n"); printf("[S]earch For A Record\n"); printf("[E]nter A New Record\n"); printf("[P]rint a Record\n"); printf("[Q]uit\n"); printf("Press d,s,e,p,q To Continue\n"); fflush(stdin); Temp=getchar(); Choice=Temp; switch(Choice) { case('d','D'): { for (Cnt=0;Cnt<=Max;Cnt++) { Display_Short(Customer[Cnt]); } break; } case('e','E'): { Temp=Check_File(); if (Temp >= 100) { printf("there are 100 records already"); } else { Enter_Record(Max); } break; } case('s','S'): { fflush(stdin); printf("Please Enter The Last Name of the Customer\n"); gets(Name_Wanted); for (Cnt=0;Cnt<=Max;Cnt++) { if (strcmp(Customer[Cnt].Last_Name,Name_Wanted)==0) { Display_Long(Customer[Cnt]); } } break; } case('p','P'): { fflush(stdin); printf("Please Enter The Last Name of the Customer\n"); gets(Name_Wanted); for (Cnt=0;Cnt<=Max;Cnt++) { if (strcmp(Customer[Cnt].Last_Name,Name_Wanted)==0) { Print_Record(Customer[Cnt]); } } break; } case('q','Q'): { printf("Thanks For Using LittleBoy"); return(0); } default: { printf("Invalid Entry"); continue; } } }while(Choice != ('q','Q')); fclose(FilePtr); return (0); } int Open_File(void) /*Opens the File for use*/ { if ((FilePtr = fopen("a:\Maillist.dat","r+b")) == NULL) { printf("\n\n Error: could not open the requested file."); exit(1); } return (0); } struct Record Initalize_Array() /*Loads the file into an Array*/ { struct Record File; fread(&File,sizeof(struct Record),1,FilePtr); return (File); } int Check_File(void) /*Checks how many Records are in the file*/ { struct Record Temp; int Cnt=0; while(fscanf(FilePtr, "%s", Temp.Last_Name) != EOF) { fseek(FilePtr,(1L*(Cnt)*sizeof(struct Record)),SEEK_SET); Cnt++; } return(Cnt); } struct Record Fill(struct Record Temp) /*Subrutine to enter all the*/ { /*information for a new record*/ fflush(stdin); do { printf("Last Name: "); gets(Temp.Last_Name); if (strlen(Temp.Last_Name) > 20) { printf("Name exceeds 20 Characters\n"); } }while (strlen(Temp.Last_Name) > 20); do {fflush(stdin); printf("First Name: "); gets(Temp.First_Name); if (strlen(Temp.First_Name) > 20) { printf("Name exceeds 20 Characters\n"); } }while (strlen(Temp.First_Name) > 20); do {fflush(stdin); printf("Address: "); gets(Temp.Address); if (strlen(Temp.Address) > 30) { printf("Address exceeds 30 Characters\n"); } }while (strlen(Temp.Address) > 30); do {fflush(stdin); printf("City: "); gets(Temp.City); if (strlen(Temp.City) > 20) { printf("City Name Exceeds 20 Characters\n"); } }while (strlen(Temp.City) > 20); do {fflush(stdin); printf("Province Code: "); gets(Temp.Province_Code); if (strlen(Temp.Province_Code) > 3) { printf("Pronvince Code Exceeds 3 Characters\n"); } }while (strlen(Temp.Province_Code) > 3); do {fflush(stdin); printf("Postal Code: "); gets(Temp.Postal_Code); if (strlen(Temp.Postal_Code) > 6) { printf("Postal Code Exceeds 6 Characters\n"); } }while (strlen(Temp.Postal_Code) > 6); do {fflush(stdin); printf("Telephone Number: "); gets(Temp.Telephone); if (strlen(Temp.Telephone) > 15) { printf("Phone Number Exceeds 15 Characters\n"); } }while (strlen(Temp.Telephone) > 15); do {fflush(stdin); printf("Year & Model: "); gets(Temp.Year_And_Model); if (strlen(Temp.Year_And_Model) > 20) { printf("Year and Model Exceed 20 Characters\n"); } }while (strlen(Temp.Year_And_Model) > 20); return(Temp); } int Write_To_File(struct Record File) /*Adds a record to file*/ { fseek(FilePtr,0,SEEK_END); fwrite(&File, sizeof(struct Record),1,FilePtr); return (0); } int Enter_Record(int Max) /*Subrutine to for entering*/ { /*a customer's information*/ struct Record Temp; Temp=Fill(Temp); Write_To_File(Temp); return (0); } void Display_Short(struct Record Temp) /*Displays all First & Last Names*/ { printf("\n%s %s", Temp.Last_Name,Temp.First_Name); } void Display_Long(struct Record Temp) /*Displays All the information*/ { printf("\n\n%s %s\n",Temp.Last_Name,Temp.First_Name); printf("%s %s\n",Temp.Address,Temp.City); printf("%s %s\n",Temp.Province_Code,Temp.Postal_Code); printf("%s\n",Temp.Telephone); printf("%s\n",Temp.Year_And_Model); } int Print_Record(struct Record Temp) /*Prints a Record*/ { FILE *Printer; Printer = fopen("PRN","w"); fprintf(Printer,"%s %s\n",Temp.Last_Name,Temp.First_Name); fprintf(Printer,"%s %s\n",Temp.Address,Temp.City); fprintf(Printer,"%s %s\n",Temp.Province_Code,Temp.Postal_Code); fprintf(Printer,"%s\n",Temp.Telephone); fprintf(Printer,"%s\n",Temp.Year_And_Model); fclose(Printer); return(0); }