Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits\stdc++.h>
- typedef struct stud
- {
- char *first_name, *last_name;
- int year, month, day;
- double av_grade;
- struct stud *next;
- };
- int main()
- {
- stud *root = NULL;
- stud *cur, *cur2;
- root = NULL;
- char file_name[128];
- char first_name[128], last_name[128], year[128], month [128], day [128], av_grade[128], new_file_name[128];
- int year_n, month_n, day_n;
- double av_grade_n;
- int action;
- while(1)
- {
- printf("1: Read the source database\n");
- printf("2: Put base into file\n");
- printf("3: Print the base into the file\n");
- printf("4: Add student from file\n");
- printf("5: Add student from keyboard\n");
- printf("6: Delete student from the base\n");
- printf("7: Search student in the base by his surname\n");
- scanf("%d", &action);
- switch(action)
- {
- case 1:
- printf("Please enter the name of the file with source data base.\n");
- scanf ("%s", file_name);
- FILE *p;
- if ((p = fopen(file_name, "rb")) == NULL)
- {
- printf("Sorry, can not open the file:(\n");
- exit(1);
- }
- while(fgets(last_name, 128, p) != NULL)
- {
- fgets(last_name, 128, p);
- fgets(first_name, 128, p);
- fgets(year, 128, p);
- year_n = atoi(year);
- fgets(month, 128, p);
- month_n = atoi(month);
- fgets(day, 128, p);
- day_n = atoi(day);
- fgets(av_grade, 128, p);
- av_grade_n = atof(av_grade);
- if(!root)
- {
- root = (stud *) malloc(sizeof(stud));
- root -> first_name = first_name;
- root -> last_name = last_name;
- root -> year = year_n;
- root -> month = month_n;
- root -> day = day_n;
- root -> av_grade = av_grade_n;
- root -> next = NULL;
- }
- for(cur = root; cur -> next; cur = cur -> next);
- cur -> next = (stud *) malloc(sizeof(stud));
- cur = cur -> next;
- cur -> first_name = first_name;
- cur -> last_name = last_name;
- cur -> year = year_n;
- cur -> month = month_n;
- cur -> day = day_n;
- cur -> av_grade = av_grade_n;
- cur -> next = NULL;
- }
- break;
- case 2:
- cur = root;
- printf("Enter filename with file extension, where you need to put structure\n");
- scanf("%s", &file_name);
- p = fopen(file_name, "wb");
- while (cur != NULL)
- {
- fprintf(p,"%s", cur -> last_name);
- fprintf(p,"%s", cur -> first_name);
- fprintf(p,"%d", cur -> year);
- fprintf(p,"%d", cur -> month);
- fprintf(p,"%d", cur -> day);
- fprintf(p,"%f", cur -> av_grade);
- cur = cur -> next;
- }
- fclose (p);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement