Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Students *head = NULL;
- struct Students *temp = NULL;
- int i =0;
- struct Students{
- char firstName[15];
- char lastName[15];
- char grade[10];
- int ID;
- double score;
- struct Students *next;
- };
- void text()
- {
- printf("--------------------------\n");
- printf("Enter a new student\n");
- printf("Check student's info\n");
- printf("Edit student's info\n");
- printf("Exit\n");
- printf("--------------------------\n");
- }
- void new()
- {
- if(i == 0){
- i++;
- head = malloc(sizeof(struct Students));
- if(head == NULL) perror("NEW HEAD ERROR");
- printf("Student's name : "); scanf("%s %s", head->firstName, head->lastName);
- printf("Student's grade : "); scanf("%s", head->grade);
- printf("Student's ID : "); scanf("%d", &(head->ID));
- printf("Student's score : "); scanf("%lf", &(head->score));
- head->next = NULL;
- }
- else{
- struct Students *student = malloc(sizeof(struct Students));
- if(student == NULL) perror("NEW STUDENT ERROR");
- temp = head;
- head = student;
- head->next = temp;
- printf("Student's name : "); scanf("%s %s", head->firstName, head->lastName);
- printf("Student's grade : "); scanf("%s", head->grade);
- printf("Student's ID : "); scanf("%d", &(head->ID));
- printf("Student's score : "); scanf("%lf", &(head->score));
- }
- }
- void check(struct Students *current, int ID)
- {
- while(current != NULL){
- if(current->ID == ID){
- printf("ID : %d\n", current->ID);
- printf("Name : %s %s\n", current->firstName, current->lastName);
- printf("Grade : %s\n", current->grade);
- printf("Score : %.2lf\n", current->score);
- break;
- }
- else
- current = current->next;
- }
- }
- int main()
- {
- int scanID = 0;
- FILE *fp = fopen("student.dat", "w");
- if(fp == NULL)
- perror("ERROR");
- printf("Welcome! Choose an action.\n");
- char *action = malloc(10*sizeof(char));
- if (action == NULL) perror("MAIN ACTION ERROR");
- text();
- while(1){
- scanf("%s", action);
- if(strcmp(action, "New")== 0){
- printf("\n");
- new();
- }
- else if(strcmp(action, "Check") == 0){
- printf("\nEnter student's ID : ");
- scanf("%d",&scanID);
- check(head, scanID);
- }
- else
- {
- while(head != NULL){
- fwrite(head, sizeof(struct Students), 1, fp);
- if(fwrite != 0)
- printf("contents to file written successfully !\n");
- else
- printf("error writing file !\n");
- head = head->next;
- }
- fclose(fp);
- return 96;
- }
- printf("\nChoose another action : ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement