fred fwrite

This is a discussion on fred fwrite within the C Programming forums, part of the General Programming Boards category; hey I'm kinda new at this so i wonder if anyone can help me. I have wrote the main code ...

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

    fred fwrite

    hey I'm kinda new at this so i wonder if anyone can help me. I have wrote the main code for the program but now I have to modify it so change, update and display data. here is the code:

    Code:
    // Students.c - version 1
    
    #include <stdio.h>
    #include <string.h>
    
    typedef struct {
       int number;
       char name[20];
       char courseCode[6];
    } Student;
    
    /* post - condition:- students' details are obtained from file and the 
                         lastindex position that was used is returned, if file 
                         exists. If the file does not already exist return -1*/
    int populateStudents(char fileName[], Student students[]);
    
    /* post-condition:- menu option is obtained from the user */
    int mainMenu();
    
    /* pre-condition:- lastIndex is the highest index of students. */
    void printStudents(Student students[], int lastIndex);
    
    /* pre-condition:- lastIndex is the highest index of students
       post-conditoin:- returns the index position for where the ID was found, 
       otherwise returns -1 */
    int findStudent(Student students[], int lastIndex, int id);
    
    /* pre-condtion:- receives the memory address where to store the number
       post-condition:- changes the contents in the memory address with number the 
       user enters */
    void getNumber(int *number);
    
    /* pre-condition:- receives the memory address of a string, and the maximum 
                       length of the string 
       post-condition:- changes the contents of the string */
    void getString(char s[], int size);
    
    /* pre-condition:- receives the memory address of a string
       post-condition:- changes the \n in the string with /0 if zn exists, 
       otherwise leaves the string unchanged */
    void removeNewline(char s[]);
    
    /* pre-condition:- the student array contains details for at least one student
       post-condition:- the file is written with details of each student, and any 
                        existing content is overwritten. */
    void saveAll(char fileName[], Student students[], int lastIndex);
    
    
    int main()
    {
       int numOfStudents = 5;
       Student students[numOfStudents]; // array to store 5 students' details
       int lastIndex = -1; // used to store the last index of students array that 
                           // was used
       
       char fileName[] = "student.dat"; // filename to be used for read and write
          
       lastIndex = populateStudents(fileName, students);  
       
       int option; // used to store the menu option
       // returns until the user enters the exit menu option
       do 
       {
          option = mainMenu();
          // used if the user enters the option for adding a new student
          if (option == 1)
          {      
             system("cls");
             printf("Add new student\n");
             // checks that the array is full
             if (lastIndex == numOfStudents-1)
             {
                printf("Unable to add any more students\n");
                system("pause");
             }
             // if the array is not full
             else
             {   
                lastIndex++; // add 1 to the contents of lastIndex
                getNumber(&students[lastIndex].number);
             
                printf("Enter name: ");
                getString(students[lastIndex].name, 
                          sizeof(students[lastIndex].name));
             
                printf("Enter course code: ");
                getString(students[lastIndex].courseCode, 
                                   sizeof(students[lastIndex].courseCode));  
             }
          }
          // used if the user enters the option for displaying students' details
          else if (option == 2)
          {
             printStudents(students, lastIndex);
             system("pause");
          }
          // used if the user enters the option for update student's details
          else if (option == 3)
          {
              // prompts the user the enter a student ID
              printStudents(students, lastIndex);
              printf("Enter ID to change: ");
              int id; // used to store the student ID
              scanf("%i", &id);
              _flushall();
              
              // check ID exists
              int found = findStudent(students, lastIndex, id);
              // performed if the student ID does not exist
              if (found == -1)
              {
                  printf("Student ID was not found\n");
                  system("pause");
              }
              // performed if the student ID was found
              else
              {
                 printf("Student found\n");
                              
                 // display option choices for update
                 printf("1 - name, 2 - Course Code: ");
                 int updateOption; // used to store the option for which attribute 
                                   // the user would like to modify 
                 scanf("%i", &updateOption);
                 _flushall();
                 // performed if the user wants to modify the name of the student
                 if (updateOption == 1)
                 {
                    // get new name
                    printf("Enter new name: ");
                    getString(students[found].name, sizeof(students[found].name));
                 }
                 // performed if the user wants to modify the course code for the 
                 // student
                 else if (updateOption == 2)
                 {
                    // get new course code
                    printf("Enter new course code: ");
                    getString(students[found].courseCode, sizeof(students[found].
                              courseCode));
                 
                 }
              } 
          }
          // performed if the user enters the option for exiting the program
          else if (option == 4)
          {    
              saveAll(fileName, students, lastIndex);
          }
       } while (option != 4);
          
       system("pause");
       return 0;
    }
    
    int mainMenu()
    {
        int option;
        system("cls");
        printf("Student Registrations\n\n");
        printf("1: Add new student\n");
        printf("2: Display students' details\n");
        printf("3: Edit student's course code\n");
        printf("4: Exit\n");
        printf("Enter option: ");
        scanf("%i", &option);
        _flushall();
        return option;
    }
    
    int populateStudents(char fileName[], Student students[])
    { 
       FILE *studentFile;
       // try to open the file in read binary mode 
       studentFile = fopen(fileName, "rb"); // open the file in read binary file
       
       int lastIndex = -1;      
       // checks if the file exists or not.
       // if the file exists, retrieve the information from the file       
       if (studentFile != NULL) 
       {   
          int i = -1;
          // obtain the information relating to the number of students' details 
          // that have been saved
          fread(&lastIndex, sizeof(int), 1, studentFile); 
          // retrieve each student's details
          while (i < lastIndex) 
          {  
             i = i + 1;
             fread(&students[i], sizeof(Student), 1, studentFile);
          }
          fclose(studentFile);   
       }
       return lastIndex;
    }
    
    // displays all the students' details on the screen
    void printStudents(Student students[], int lastIndex)
    {
       int i;
       system("cls");
       printf("Students' details\n\n");
       // performed if the students array contains at least one record
       if (lastIndex >= 0)
       {            
          printf("ID\tName\t\tCourse Code\n");
          // repeats until all of the students' details have been displayed
          for (i = 0; i <= lastIndex; i++) 
          {
             printf("%i\t%s\t\t%s\n", students[i].number, students[i].name, 
                               students[i].courseCode);
          }
       }
    }
    
    // searches through the array until a match for the student ID is found, or 
    // until the end of the array has been reached
    // returns the index where the student ID was found, otherwise returns -1
    int findStudent(Student students[], int lastIndex, int id)
    {  int i;
       // repeats while the contents of i is less than or equal to the contents of 
       // lastIndex
       for (i = 0; i <= lastIndex; i++)
       {         
          // checks if the contents of id is the same as the student's id.  A match 
          // has been made
          if (id == students[i].number)
          {
             return i; // returns the position where the student id as found
          }
       }
       system("pause");
       return -1; // returns a non-index value, the id was not found in the array
    }
    
    void saveAll(char fileName[], Student students[], int lastIndex)
    {
       // checks that the array has at least one record to save
       if (lastIndex >= 0)
       {
          FILE *studentFile = fopen(fileName, "wb"); // open the file in write 
                                                     // binary mode
          int i;
        
          // save the contents of lastIndex so the program will know how many 
          // records need to be read back from the file 
          fwrite(&lastIndex, sizeof(int), 1, studentFile);
          // repeat until all the students' details have been saved to file
          for (i = 0; i <= lastIndex; i++) 
          {     
             fwrite(&students[i], sizeof(Student), 1, studentFile);
          }
          fclose(studentFile); // close the file
       }
    }
    
    void getNumber(int *number)
    {
         printf("Enter student number ");
         scanf("%i", number);
         _flushall();
    }
    
    void getString(char s[], int size)
    {
         fgets(s, size, stdin);
         removeNewline(s);
    }
    
    // function to remove \n for the end of the string, if \n exists
    void removeNewline(char s[])
    {
        // find the length of the string (this will include \n if 
        // it exists) and then subtract 1 from the length
        int i = strlen(s) - 1;
        // check if the character at position 1 is \n
        if (s[i] == '\n')
        {              
            // replace \n with \0           
            s[i] = '\0';
        }
    }
    Thanx in advance

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    507
    So you want us to write it for you or something? I don't understand, I don't see a question in your post.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    fread fwrite

    if possible lol. i just dont understand in which place should i put the code for the program to do the above things

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using fwrite
    By kenkoh in forum C Programming
    Replies: 10
    Last Post: 02-16-2008, 10:34 AM
  2. WriteFile and fwrite
    By George2 in forum C Programming
    Replies: 4
    Last Post: 08-10-2007, 05:33 AM
  3. fwrite error: bad address
    By icebabe in forum C Programming
    Replies: 6
    Last Post: 05-06-2006, 07:35 AM
  4. Determining fwrite errors
    By cboard_member in forum C Programming
    Replies: 2
    Last Post: 04-26-2006, 06:30 AM
  5. fwrite file size
    By lambs4 in forum C Programming
    Replies: 4
    Last Post: 04-04-2003, 05:49 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21