1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Records and Files

Discussion in 'C' started by bouki, Apr 9, 2010.

  1. bouki

    bouki New Member

    Joined:
    Apr 9, 2010
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    This topic is really new to me, i was just reading some stuff ahead of my class and saw a practisable question. However i have started it and stuck here. See question below.

    QUESTION

    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. The students should create appropriate functions to solve the problem.

    Three Questionss
    1. Why when it is printed i get rubbish?
    2. When i search for the ID, how can i display the entire information for that ID?
    3. Im lost to how i am suppose to update the file
    4. Is the loading function done correctly

    PHP:
    #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]={
    {
    234,"Spencer""Sochelle"220587"29_Decent_Village""528-5214""Business"},         
    {
    345,"Dobson""Dwayne"890583"263_Far Park Blvd""457-2014""Computer"},
    {
    456,"Clarke""Dave"110181"87_Rasta_Village""354-5874""Law"},
    {
    567,"Currie""Nickeisha"230491"26_Waterground_rd""898-6578""Nurse"},
    {
    678,"Blackwood""Mariann"10490"3_St_Johns_road""256-1458""Art"},
    {
    789,"Hall""Wesley"101086"3_Bayfarm_road""236-1028""Science"},
    {
    891,"Hall""Bouki"251285"67_pimpim_drive""887-9910""Engineer"},
    {
    912,"Paul""Shawn"231180"25_glasco_Close""567-1996""Law"},
    {
    101,"Able""Frank"100591"1_Camp_road""528-1235""Business"},
    };

    char GetUserOption();
    int GetIDFromUser();
    void LetUserSearchForStudent();
    void UpdateStudents(Student students[]);
    void PrintStudents(Student students[]);
    void LoadStudents(Student students[]);
    void SaveStudents(Student students[]);


    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;
    }

    int GetIDFromUser()
    {
      
    int id 0;
      
    printf("Enter the ID: ");
      
    scanf("%d", &id);
      return 
    id;
    }

    void LetUserSearchForStudent()
    {
      
    // Getting the ID from the user to search for
      
    int id GetIDFromUser();
    }





    // students must hold 10 students
    void UpdateStudents(Student students[])
    {
        





    // students must hold 10 students
    void PrintStudents(Student students[])
    {

    FILE *fp ;
    char ch ;

    fp fopen "records.txt""rb" ) ;

    while ( 
    )
    {
    ch fgetc fp) ;

    if ( 
    ch == EOF )
    break ;

    printf "%c"ch ) ;
    }
    fclose fp ) ;
    }


    // students must hold 10 students
    void LoadStudents(Student students[])
    {
      
      
    FILE *fp;
      
    fp=fopen("records.txt","a+");
    }



    // 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(010; ++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':
            
    UpdateStudents(students);
            break;

          case 
    'P':
            
    PrintStudents(students);
            break;

          case 
    'S':
            
    LetUserSearchForStudent();
            break;

          case 
    'E':
            
    looping 0// exit the loop
            
    break;
        }
      } 

      
    // Save the students to the file
      
    SaveStudents(students);

      return 
    0;
    }
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Then I suggest you focus on where you're at, rather than trying to rush ahead. Maybe a future lesson will teach you what you need to know in order to solve this question. If you want more work then ask your tutor for something; he may have stuff prepared. If you've *really* got no other work to do then pop down the bar and finish off their cider for them; that's what we used to do (well, almost; we didn't usually leave it until we had no work to do...)
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Anyway I guess what you really wanted was some useful help.

    > 1. Why when it is printed i get rubbish?

    When what is printed? What option did you choose? What output did you get and what output did you expect?
    Have you identified the line of code that prints wrong stuff?

    If the output is unprintable and you're using a Unix-style OS then redirect the output to a file and use od -x -c to display the output. Or just pipe the output to od -x -c, but sending it to a file gives you the option of keeping it around for future reference.

    What exactly does LoadStudents() do? Specifically the "a+" parameter of fopen? (Note I'm not asking because I don't know. I'm asking because I want *you*, Mr Smart Alec, to find the answer out for yourself)

    2. When i search for the ID, how can i display the entire information for that ID?

    Maybe with printf()? Could there be something in SaveStudents() that you could use? (Look again if you think the answer to that is "no")

    3. Im lost to how i am suppose to update the file

    Probably that will be the subject of a future lesson. Have a look at LoadStudents(). It seems to be missing a lot of stuff. Is it meant to read the file into a data structure? Typically what you do in a program is to read a file into some internal data structures, process the data, and if you need to update the file on disk then you would write the data structures back out to the file. So in effect you don't update the file directly. Since the data is not fixed length you don't have the option of using fseek and fwrite, so you'll have to overwrite the whole file from the data structures.

    4. Is the loading function done correctly

    I've already alluded to one possible answer to that question. But what do you think? What should the function do? What does it actually do (hint: RTFM for fopen())? Do you think it might need to mirror SaveStudents()?
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    To be honest though I think a *better* use of your time would be to
    (a) think up some variations on the exercises you've already been given;
    (b) see if you can help other students who are stuck; explaining stuff that you think you understand and fielding their questions is a great way to consolidate what you know and to learn new stuff. I learn new stuff all the time posting here; often a question will trigger some interest and I'll go off and look it up. And I'm not exactly new to this; I started programming in about 1981.
     

Share This Page