Simple name and number query system and name query system
1 # include <stdio. h> 2 # include <string. h> 3 # include <stdlib. h> 4 struct student // define struct 5 {6 char name [7]; // name 7 int number; // number 8} student, student1; 9 void menu () // display Column 10 {11 printf ("************************* \ n "); 12 printf ("1 input record \ n"); 13 printf ("2 search record \ n"); 14 printf ("0 quit system \ n "); 15} 16 void save () // Input Function 17 {18 FILE * fp; 19 system ("cls"); // clear screen 20 fp = fopen ("filename ", "a +"); 21 if (Fp = NULL) 22 {23 printf ("open file error"); 24 exit (1); 25} 26 while (1) 27 {28 scanf ("% s % d", student. name, & student. number); // enter the name and number 29 fwrite (& student, sizeof (struct student), 1, fp); 30 printf ("continue? (Y/n) "); // whether to continue 31 getchar (); 32 if (getchar () = 'n') break; 33} 34 fclose (fp ); 35 exit (0); 36} 37 void search () // search function 38 {39 40 FILE * fp; 41 char name [7]; 42 system ("cls "); // clear the screen 43 if (fp = fopen ("filename", "r") = NULL) 44 {45 printf ("open the file error "); 46 exit (1); 47} 48 printf ("please int your name \ n"); 49 scanf ("% s", name); 50 while (fread (& student1, sizeof (struct student), 1, fp) = 0) // search for 51 {52 if (strcmp (student1.name, name) = 0) 53 {54 printf ("% s % d", student1.name, student1.number); // display the name and number 55 break; 56} 57} 58 if (feof (fp) // check whether the file Pointer Points to the end of the file 59 {60 printf ("search fail "); // If the query is finished, the output query fails. 61 exit (0); 62} 63} 64 int main () // The main function 65 {66 int select; 67 menu (); // display the main menu 68 printf ("\ nPlease enter your select \ n"); 69 scanf ("% d", & select); 70 while (select) 71 {72 switch (select) 73 {74 case 1: save (); break; 75 case 2: search (); break; 76 case 0: exit (1 ); 77} 78} 79}