Thread: Structures and Files

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    14

    Question Structures and Files

    You are required to create a student management system that will allow you to store and
    update student records. Each student record should consist of an id, a name (fist name
    and last name), date of birth, address, telephone number and the program being pursued.
    Assume 10 records will be stored.
    1. Create and populate an array of records. Store records into file
    2. Allow user to update a record. Note, the user should only manipulate the data
    structure. All changes to the data structure should then be use to overwrite the
    file.
    3. Allow user to search for a record from the data structure.
    4. Print all records from the structure.
    NB. You should create appropriate functions to solve the problem.

    I HAVE BEEN TRYING TO COMPLETE THIS PROGRAM AND I THINK I NEED ONLY THREE FUNCTION TO COMPLETE.
    1. update
    2. print from the file
    3. load students from file

    Im not sure how to have them done. Will appreciate the help

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    struct Student
    {
      int ID;
      char LastName[32]; 
      char FirstName[32];
      int DateOfBirth; // This could be of the form YYYYMMDD. For example someone born on December 21st, 1990 would have a value of 19901221
      char Address[32];
      char TelephoneNumber[11]; // a 10-digit string
      char ProgramPursued[32];   
    };
    
    struct student students[10]={
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a243","Spencer", "Sochelle", "22-05-87", "29_Decent_Village", "898-0497", "business"},         
    {"a123","Dobson", "Dwayne", "89-05-83", "263_Far Park Blvd", "457-2014", "Computer"},
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    };
    
    
    char GetUserOption()
    {
      char option = 'I'; // 'I' for Invalid
    
      while(option == 'I')
      {
        // Print the menu items
        printf("\n");
        printf("Choose one of the following options:\n[u]pdate   [P]rint   [S]earch   [E]xit\n");
        scanf("%c", &option);
    
        switch(toupper(option))
        {
          case 'U':
          case 'P':
          case 'S':
          case 'E':
            break;
          default:
            option = 'I';
            break;
        }
      }
    
      return option;
    }
    
    void LetUserSearchForStudent()
    {
      // Getting the ID from the user to search for
      int id = GetIDFromUser();
    }
    
    // Getting an "ID" from the user
    int GetIDFromUser()
    {
      int id = 0;
      printf("Enter the ID: ");
      scanf("%d", &id);
      return id;
    }
    
    // students must hold 10 students
    void UpdateStudents(Student students[])
    {
      // TODO: update the file
    }
    
    // students must hold 10 students
    void PrintStudents(Student students[])
    {
      // TODO: print students from file
    }
    
    // students must hold 10 students
    void LoadStudents(Student students[])
    {
      // TODO: load students from file
    }
    
    
    // students must hold 10 students
    void SaveStudents(Student students[])
    {
      int i;
    
      // Open the file for writing
      FILE *fp = fopen("records.txt", "wb");
      if(fp == NULL) return; 
    
      // Loop through each student
      for(i = 0; i < 10; ++i)
      {
        // Write the student to the file....here we will use comma-separated values
        fprintf(fp, "%d,%s,%s,%d,%s,%s,%s\r\n", 
                students[i].ID,
                students[i].LastName,
                students[i].FirstName,
                students[i].DateOfBirth,
                students[i].Address,
                students[i].TelephoneNumber,
                students[i].ProgramPursued
        );    
      }
    
      // Close the file
      fclose(fp);  
    }
    
    
    int main()
    {
      Student students[10];
      int looping = 1;
    
      // Load the students from the file
      LoadStudents(students);
    
      // Loop until exit
      while(looping)
      {
        char option = GetUserOption();
        switch(option)
        {
          case 'U':
            // TODO: Let the user update a record
            break;
    
          case 'P':
            // TODO: Print the students to the screen
            break;
    
          case 'S':
            LetUserSearchForStudent();
            break;
    
          case 'E':
            looping = 0; // exit the loop
            break;
        }
      } 
    
      // Save the students to the file
      SaveStudents(students);
    
      return 0;
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Let's see your try first. Nobody is going to do your work for you, but we will assist with any problems you encounter or errors that occur.

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Write out some pseudo code. How would you do this without any code?

    1. Create and populate an array of records. Store records into file

    -Seems you are working off a static structure and not a dynamic one? Can the user populate this list of 10 students right from the start? Are is the user only to edit an existing student body list?

    2. Allow user to update a record. Note, the user should only manipulate the data
    structure. All changes to the data structure should then be use to overwrite the
    file.

    It seems as if you know how to access each student. I would add in some scanf() in there for the user to make the necessary changes to each structure member as appropriate.

    Write your changes to a new file for your new additions, then delete/clean up your original file.

    3. Allow user to search for a record from the data structure.

    Again it appears you know how to read in a file. Simply search the file for the attribute in search, either by name or address, then print out the complete student[i] structure.
    Using strcmp() comes to mind here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. How to create a C project with multiple source files
    By MSF1981 in forum C Programming
    Replies: 5
    Last Post: 03-22-2009, 09:25 AM
  3. Random access files and structures
    By DLR in forum C Programming
    Replies: 8
    Last Post: 04-21-2006, 03:24 PM
  4. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM
  5. structures and files
    By eth0 in forum C++ Programming
    Replies: 9
    Last Post: 01-06-2004, 06:48 PM