Page 1 of 1

2.1 Task 1 [2 marks] In the staff.c file, in the "open_file()" function, write code to open/create an output file. You s

Posted: Tue Jul 12, 2022 8:04 am
by answerhappygod
2 1 Task 1 2 Marks In The Staff C File In The Open File Function Write Code To Open Create An Output File You S 1
2 1 Task 1 2 Marks In The Staff C File In The Open File Function Write Code To Open Create An Output File You S 1 (31.82 KiB) Viewed 111 times
/*=======================================================================================* * File: staff.c * ---------------------------- * .c file of staff library. All function prototype instaff.h. * * You can add local functions if required. Insert theprototype and implementations * between the marked lines. * *=======================================================================================*/
#include <stdio.h>#include "staff.h"
struct StaffInfo loaded_staff_data[10]/* Remove this line and line 23 to uncomment if required = {{123, "Raju Abhijit", "Ipoh", 2020, "Junior Engineer",3000}, {104, "Sharifah Al-Amir", "Kuala Lumpur", 2019,"Engineer", 3500}, {254, "Sanjana Suman", "Jeli", 2016, "HR Officer",4750}, {256, "Chin Qiang Lian", "Tawau", 2021, "SiteEngineer", 2500}, {165, "Yahya Hamid", "Rasa", 2016, "Senior Engineer",8750}, {155, "Jeremy Chew", "Bintangor", 2020, "LogisticsOfficer", 3000}, {654, "Mohamad Ali", "Ipoh", 2018, "Secretary",3500}, {855, "Eu Sze Kuan", "Kemaman", 2017, "Accountant",7000}, {655, "Stanley Khoo", "Dungun", 2020, "SafetyOfficer", 4000}, {654, "Amir Hamzah", "Rasa", 2019, "Technician",3500}} * Remove this line and line 12 to uncomment */;
/*********** Local Function Prototype and Implementation***********//* You can add local function prototypes and implementation below *//*******************************************************************///<------------------- START editing here------------------------>//
//<-------------------- STOP editing here------------------------>////----- End of Local Function Prototypes and Implementations-------/
/*---------------------------------------------------------------------------------------* * Function: open_file * -------------------- * Opens a file named with your matric number. Do not closethe created file * * Arguments: None * * Returns: File pointer to created file (do not edit) * Returns NULL if filecreation fails * * IMPORTANT! Only edit between the marked lines. DO NOT CLOSETHE CREATED FILE. *---------------------------------------------------------------------------------------*/FILE *open_file(void) { FILE *fp = NULL; //<---------------------- START editing here-------------------->//
//<----------------------- STOP editing here-------------------->// return fp;}
/*---------------------------------------------------------------------------------------* * Function: enter_staff_info * -------------------- * Enters staff info in the structure. THe main function willcall this function every * time there is a new staff info to be entered. You DO NOThave to iterate for multiple * staff data entry. * * Arguments: struct StaffInfo pointer - points to staff infoin StaffInfo structure * * Returns: None * * IMPORTANT! Only edit between the marked lines. *---------------------------------------------------------------------------------------*/void enter_staff_info(struct StaffInfo *staff_info) { printf("Begin staff info entry...\n\n"); //<---------------------- START editing here-------------------->//
//<----------------------- STOP editing here-------------------->//}
/*---------------------------------------------------------------------------------------* * Function: print_staff_info * -------------------- * Prints newly entered staff info from the StaffInfostructure. This function will be * called by the main function for every staff info entered.You DO NOT have to iterate * to print multiple staff info. * * Arguments: struct StaffInfo - staff info structure * * Returns: None * * IMPORTANT! Only edit between the marked lines. *---------------------------------------------------------------------------------------*/void print_staff_info(struct StaffInfo staff_info) { printf("\n\nPrinting new staff info...\n\n"); //<---------------------- START editing here-------------------->//
//<----------------------- STOP editing here-------------------->//}
/*---------------------------------------------------------------------------------------* * Function: store_staff_info * -------------------- * Stores newly entered staff info into file. The mainfunction will call this function * for every staff info entered. You DO NOT have to iteratefor every staff info. * * Arguments: FILE *fp - pointer to file where the informationwill be stored * struct StaffInfo -structure containing staff info * * Returns: None * * IMPORTANT! Only edit between the marked lines. *---------------------------------------------------------------------------------------*/void store_staff_info(FILE *fp, struct StaffInfo staff_info){ printf("\n\nStoring new staff info infofile...\n\n"); //<---------------------- START editing here-------------------->//
//<----------------------- STOP editing here-------------------->//}
/*---------------------------------------------------------------------------------------* * Function: load_staff_file * -------------------- * Loads staff information file. This function should openexisting staff info file and * inserts it into the struct StaffInfo loaded_staff_data[10]array as declared at the * top of this (staff.c) file. * * If you choose to skip implementation of this function, youcan proceed with next task * by uncommenting lines 15 - 26 above (delete line 15 and26). You may need to change * your struct StaffInfo elements to accommodate the defaultdata. * * Arguments: None * * Returns: File pointer to created file (do not edit) * Returns NULL if filecreation fails * * IMPORTANT! Only edit between the marked lines. *---------------------------------------------------------------------------------------*/FILE *load_staff_file(void) { FILE *fp = NULL; // Variable to store file pointer to the opened staffinfo // file
printf("\nStarting staff info fileload...\n\n"); //<---------------------- START editing here-------------------->//
//<----------------------- STOP editing here-------------------->// return fp;}
/*---------------------------------------------------------------------------------------* * Function: calculate_salary_difference * -------------------- * Calculates salary difference of staff compared to staffsalary based on experience. The * expected salary can be calculated as follows: * Expected salary = basic *(1.1)^(years of experience) * This function stores all the information of the staff intothe file created in * open_file(). * * Arguments: FILE *fp - file pointer to the created file inopen_file * int new_staff_no -total number of new staff info added * struct StaffInfo*staff_info - pointer to an array of StaffInfo structure. * You have to iterate through the array elements toaccess each staf * info. * * Returns: None * * IMPORTANT! Only edit between the marked lines. *---------------------------------------------------------------------------------------*/void calculate_salary_difference (FILE *fp, int new_staff_no,struct StaffInfo *staff_info) { printf("\nCalculating staff salarydifference...\n\n"); //<---------------------- START editing here-------------------->// if (new_staff_no == 0) { // process only loaded data
} else { // process both entered and loadeddata
} //<----------------------- STOP editing here-------------------->//}
2.1 Task 1 [2 marks] In the staff.c file, in the "open_file()" function, write code to open/create an output file. You should name the file with your matric number (e.g. 1839303839.txt). If your matric number contains a dash, please remove it (e.g. matric no: 1834934344-1 File name: 18349343441.txt. DO NOT CLOSE the file. Then, in the first line, insert your matric number. The content of the created file should look like Figure 3. 1839303839.txt - Notepad File Edit Format View Help 1839303839 Figure 3. File content.