Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "student.h"
- // Allocate enough memory to store a new student then return a pointer to it
- student_t * initialize_student() {
- student_t *student = NULL;
- student = (student_t*)malloc(sizeof(student));
- return student;
- }
- // Creates a student based on user input and returns a pointer to that student
- student_t * create_student() {
- student_t *student = initialize_student();
- if(student) {
- printf("Enter student id:\n\n");
- scanf("%i", &student->id);
- getchar(); // Remove the newline from the buffer
- printf("Enter student name:\n\n");
- fgets(student->name, sizeof(student->name), stdin);
- student->name[(size_t)strlen(student->name) - 1] = '\0'; // Remove the newline from end of the array
- printf("Enter student age:\n\n");
- scanf("%i", &student->age);
- getchar(); // Remove the newline from the buffer
- } else {
- printf("No student to assign information to, please ensure that the student has been initialized.\n");
- }
- return student;
- }
- // Reads information from a text file and returns a pointer to a student with that information
- student_t * read_student_file() {
- student_t *student = initialize_student();
- if(student) { // Ensure the program was able to allocate memory for the student
- FILE *f = fopen("student_reads.txt", "r");
- if(f) { // Ensure the program was able to find the text file
- fscanf(f, "%i\n", &student->id);
- fgets(student->name, sizeof(student->name), f);
- student->name[strlen(student->name) - 1 ] = '\0';
- fscanf(f, "%i\n", &student->age);
- } else {
- printf("Error opening text file. Please review the file path to ensure it is set correctly.\n");
- }
- fclose(f);
- } else {
- printf("No student to assign information to, please ensure that the student has been initialized.\n");
- }
- return student;
- }
- // Reads information from a previously created student and writes that information to a text file
- void write_student_file( student_t * student ) {
- if(student) { // Ensure the program was able to allocate memory for the student
- FILE *f = fopen("student_write.txt", "w");
- fprintf(f, "%i\n", student->id);
- fprintf(f, "%s\n", student->name);
- fprintf(f, "%i\n", student->age);
- printf("Information has been written to 'student_write.txt'\n\n");
- fclose(f);
- } else {
- printf("No student to assign information to, please ensure that the student has been initialized.\n");
- }
- free(student); // Free the memory previously allocated for that student
- }
- // Print information about a given student
- void print_student( student_t * student ) {
- if(student) { // Ensure the program was able to allocate memory for the student
- printf("Student id: %i\n", student->id);
- printf("Name: %s\n", student->name);
- printf("Age: %i\n\n", student->age); // Extra newline for better formatting
- } else {
- printf("No student to read information from, please ensure that the student has been initialized\n");
- }
- free(student); // Free the memory previously allocated for that student
- }
- // Prints a menu of options that loops continually until the user chooses to exit the program.
- void show_menu() {
- int user_choice;
- do {
- printf("1. Read student information from file\n");
- printf("2. Write student information to file\n");
- printf("3. Exit\n\n"); // Extra newline for better formatting
- scanf("%i", &user_choice);
- switch(user_choice) {
- case 1:
- print_student( read_student_file() );
- break;
- case 2:
- write_student_file( create_student() );
- break;
- case 3:
- printf("You have chosen to exit the program\n");
- break;
- default: // Handle wrong input from the user
- printf("Error, please review that you entered a number between 1 and 3\n");
- }
- } while(user_choice != 3);
- }
Advertisement
Add Comment
Please, Sign In to add comment