Moortiii

Simpler Hand-in_5_2

Oct 29th, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "student.h"
  5.  
  6. // Creates a student based on user input and returns a pointer to that student
  7. student_t * create_student() {
  8.     student_t *student = (student_t*)malloc(sizeof(student));
  9.     printf("Enter student id:\n\n");
  10.     scanf("%i", &student->id);
  11.     getchar(); // Remove the newline from the buffer
  12.  
  13.     printf("Enter student name:\n\n");
  14.     fgets(student->name, sizeof(student->name), stdin);
  15.     student->name[(size_t)strlen(student->name) - 1] = '\0'; // Remove the newline from end of the array
  16.  
  17.     printf("Enter student age:\n\n");
  18.     scanf("%i", &student->age);
  19.     getchar(); // Remove the newline from the buffer
  20.     return student;
  21. }
  22.  
  23. // Reads information from a text file and returns a pointer to a student with that information
  24. student_t * read_student_file() {
  25.     student_t *student = (student_t*)malloc(sizeof(student));
  26.     FILE *f = fopen("student_read.txt", "r");
  27.     fscanf(f, "%i\n", &student->id);
  28.     fgets(student->name, sizeof(student->name), f);
  29.     student->name[strlen(student->name) - 1] = '\0';
  30.     fscanf(f, "%i\n", &student->age);
  31.     fclose(f);
  32.     return student;
  33. }
  34.  
  35. // Reads information from a previously created student and writes that information to a text file
  36. void write_student_file( student_t * student ) {
  37.     FILE *f = fopen("student_write.txt", "w");
  38.     fprintf(f, "%i\n", student->id);
  39.     fprintf(f, "%s\n", student->name);
  40.     fprintf(f, "%i\n", student->age);
  41.     printf("Information has been written to 'student_write.txt'\n\n");
  42.     fclose(f);
  43.     free(student); // Free the memory we previously allocated for a student
  44. }
  45.  
  46. // Print information about a given student
  47. void print_student( student_t * student ) {
  48.     printf("Student id: %i\n", student->id);
  49.     printf("Name: %s\n", student->name);
  50.     printf("Age: %i\n\n", student->age); // Extra newline for better formatting
  51.     free(student); // Free the memory we previously allocated for a student
  52. }
  53.  
  54. // Prints a menu of options that loops continually until the user chooses to exit the program.
  55. void show_menu() {
  56.     int user_choice;
  57.     do {
  58.         printf("1. Read student information from file\n");
  59.         printf("2. Write student information to file\n");
  60.         printf("3. Exit\n\n"); // Extra newline for better formatting
  61.         scanf("%i", &user_choice);
  62.         switch(user_choice) {
  63.             case 1:
  64.                 print_student( read_student_file() ); // Read student information from a file and print it back out
  65.                 break;
  66.             case 2:
  67.                 write_student_file( create_student() ); // Get student information from user and write it to a file
  68.                 break;
  69.             case 3:
  70.                 printf("You have chosen to exit the program\n");
  71.                 break;
  72.             default: // Handle wrong input from the user
  73.                 printf("Error, please review that you entered a number between 1 and 3\n");
  74.         }
  75.     } while(user_choice != 3);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment