Moortiii

Hand-in_5_2

Oct 29th, 2017
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "student.h"
  5.  
  6. // Allocate enough memory to store a new student then return a pointer to it
  7. student_t * initialize_student() {
  8.     student_t *student = NULL;
  9.     student = (student_t*)malloc(sizeof(student));
  10.     return student;
  11. }
  12.  
  13. // Creates a student based on user input and returns a pointer to that student
  14. student_t * create_student() {
  15.     student_t *student = initialize_student();
  16.     if(student) {
  17.         printf("Enter student id:\n\n");
  18.         scanf("%i", &student->id);
  19.         getchar(); // Remove the newline from the buffer
  20.  
  21.         printf("Enter student name:\n\n");
  22.         fgets(student->name, sizeof(student->name), stdin);
  23.         student->name[(size_t)strlen(student->name) - 1] = '\0'; // Remove the newline from end of the array
  24.  
  25.         printf("Enter student age:\n\n");
  26.         scanf("%i", &student->age);
  27.         getchar(); // Remove the newline from the buffer
  28.     } else {
  29.         printf("No student to assign information to, please ensure that the student has been initialized.\n");
  30.     }
  31.     return student;
  32. }
  33.  
  34. // Reads information from a text file and returns a pointer to a student with that information
  35. student_t * read_student_file() {
  36.     student_t *student = initialize_student();
  37.     if(student) { // Ensure the program was able to allocate memory for the student
  38.         FILE *f = fopen("student_reads.txt", "r");
  39.         if(f) { // Ensure the program was able to find the text file
  40.             fscanf(f, "%i\n", &student->id);
  41.             fgets(student->name, sizeof(student->name), f);
  42.             student->name[strlen(student->name) - 1 ] = '\0';
  43.             fscanf(f, "%i\n", &student->age);
  44.         } else {
  45.             printf("Error opening text file. Please review the file path to ensure it is set correctly.\n");
  46.         }
  47.         fclose(f);
  48.     } else {
  49.         printf("No student to assign information to, please ensure that the student has been initialized.\n");
  50.     }
  51.  
  52.     return student;
  53. }
  54.  
  55. // Reads information from a previously created student and writes that information to a text file
  56. void write_student_file( student_t * student ) {
  57.     if(student) { // Ensure the program was able to allocate memory for the student
  58.         FILE *f = fopen("student_write.txt", "w");
  59.         fprintf(f, "%i\n", student->id);
  60.         fprintf(f, "%s\n", student->name);
  61.         fprintf(f, "%i\n", student->age);
  62.         printf("Information has been written to 'student_write.txt'\n\n");
  63.         fclose(f);
  64.     } else {
  65.         printf("No student to assign information to, please ensure that the student has been initialized.\n");
  66.     }
  67.     free(student); // Free the memory previously allocated for that student
  68. }
  69.  
  70. // Print information about a given student
  71. void print_student( student_t * student ) {
  72.     if(student) { // Ensure the program was able to allocate memory for the student
  73.         printf("Student id: %i\n", student->id);
  74.         printf("Name: %s\n", student->name);
  75.         printf("Age: %i\n\n", student->age); // Extra newline for better formatting
  76.     } else {
  77.         printf("No student to read information from, please ensure that the student has been initialized\n");
  78.     }
  79.     free(student); // Free the memory previously allocated for that student
  80. }
  81.  
  82. // Prints a menu of options that loops continually until the user chooses to exit the program.
  83. void show_menu() {
  84.     int user_choice;
  85.     do {
  86.         printf("1. Read student information from file\n");
  87.         printf("2. Write student information to file\n");
  88.         printf("3. Exit\n\n"); // Extra newline for better formatting
  89.         scanf("%i", &user_choice);
  90.  
  91.         switch(user_choice) {
  92.             case 1:
  93.                 print_student( read_student_file() );
  94.                 break;
  95.             case 2:
  96.                 write_student_file( create_student() );
  97.                 break;
  98.             case 3:
  99.                 printf("You have chosen to exit the program\n");
  100.                 break;
  101.             default: // Handle wrong input from the user
  102.                 printf("Error, please review that you entered a number between 1 and 3\n");
  103.         }
  104.     } while(user_choice != 3);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment