Question
The given files in the zipped file: Lab 4_Given_Code.zip. are a header file, a lab4.c and a text file. The Lab04.c source program consists of
The given files in the zipped file: Lab 4_Given_Code.zip. are a header file, a lab4.c and a text file.
The "Lab04.c" source program consists of the following four(4) subprograms:
int read_text(char *p_text[], FIle *fp):
This function is called by the main program to read the input "text.txt" file, line by line, and return the entire text in the array of pointers: char *p_text[MAX_NUM_LINES]. Please read the documentation in the source code of the function for details. The code for this function is given and no modifications are required by you.
void write_text_inorder(char *p_text[], nlines):
This subroutine takes the "p_text" array of pointers as argument, and the number of lines it contains (nlines). The subroutine prints the contents of the pointer array in the same order that it was input by the "read_text" function. Please read the documentation in the source code of the subroutine for details.
Task: The subroutine header is given, you are to write the body. Note: If there are no lines of text to print, you must output a message to that affect using the "MSG_INFO" enumerated message type and the report_message(...) routine.
void write_text_reverse(char *p_text[], nlines):
This subroutine takes the "p_text" array of pointers as argument, and the number of lines it contains (nlines). The subroutine prints the contents of the pointer array in the REVERSE order that it was input by the "read_text" function. That is, the last input line of text is printed first, the second-last input line is printed second, and so on. Please read the documentation in the source code of the subroutine for details.
The subroutine header is given, you are to write the body. Note: If there are no lines of text to print, you must output a message to that affect using the "MSG_INFO" enumerated message type and the report_message(...) routine.
void report_message(char msg[MAX_MSG_NUM][MAX_MSG_LNG], enum msg_kind_type msg_kind ):
This routine prints a given message, "msg", in a standardized format with each message type having a unique message prefix to identify the message on output. For example, an information message (MSG_INFO) has a prefix of "::> MSG: ", while an error message (MSG_ERROR) has a prefix of ">>> ERR: ". Please read the documentation in the source code of the subroutine for details.
Your task is to create a NEW enumerated message type/kind called "MSG_END", and use it to print the "Program Terminated. Have a great day." message in a standardized fashion using the report_message(...) routine.
The output message MUST look like this:
The message in-order The message in-reverse order ==> END: Program Terminated. : Have a great day.
ha_library.h=
char msg[MAX_MSG_NUM][MAX_MSG_LNG]; enum msg_kind_type{MSG_INFO, MSG_WARNING, MSG_ERROR, MSG_END};
#endif /* HA_LIBRARY_H_ */
lab4.c= */
#include
//===== GLOBAL FUNCTION PROTOTYPES ============================================================== int read_text (char *p_text[], FILE *fp );
void write_text_inorder(char *p_text[], int nlines );
void write_text_reverse(char *p_text[], int nlines );
void report_message (char msg[MAX_MSG_NUM][MAX_MSG_LNG], enum msg_kind_type msg_kind );
int main(int argc, char *argv[]) { //================================ setbuf(stdout, NULL);
int nlines; int return_val;
char *p_text[MAX_NUM_LINES]; char filename_in[] = "text.txt"; FILE *fp_in = NULL;
return_val = 0;
for (int i=0; i < MAX_MSG_NUM; i++) for (int j=0; j < MAX_MSG_LNG; j++) msg[i][j] = cNUL;
if ( (fp_in = fopen(filename_in, "r")) == NULL ) {
sprintf(msg[0], "%s%s", "Cannot open file ", filename_in); report_message(msg, MSG_WARNING); return_val = -1;
}else { // File open was successful! if ( (nlines = read_text(p_text, fp_in)) >= 0 ) {
write_text_inorder(p_text, nlines); // Call subprogram to print in order. write_text_reverse(p_text, nlines); // Call subprogram to print in reverse order.
}else { sprintf(msg[0], "%s", sprintf(msg[1], "%s[%d]", "Max. number of input lines allowed = ", MAX_NUM_LINES); sprintf(msg[2], "%s", report_message(msg, MSG_ERROR); return_val = -1; // Set up to return ERROR condition (-1) to OS. }
fclose(fp_in); }
printf(" ==> END: Program Terminated."); printf(" : Have a great day.");
return (return_val); // Return to OS. }
int read_text(char *p_text[], FILE *fp ) // Input file pointer. { int i; // Loop index. int nlines; int line_lng; // Length of current input line. char line[MAX_LINE_LNG]; char *p; int return_val;
nlines = 0; return_val = 0;
while ( (fgets(line, MAX_LINE_LNG, fp) != NULL) && (return_val >= 0) ) {
i = 0; while ((line[i] != cNUL) && (i <= MAX_LINE_LNG) ) { if ( (line[i] == cNL) || (line[i] == cCR) ) line[i] = cNUL; i++; }
nlines++; if ( nlines <= MAX_NUM_LINES ) { // Ensure that we have room in our p_text array of pointers for the // input line just read.
line_lng = strlen(line); if ( (p = malloc(line_lng +1)) != NULL ) { strncpy(p, line, line_lng); p[line_lng] = cNUL;
p_text[nlines -1] = p; return_val = nlines;
}else return_val = -1; }else return_val = -1; } return(return_val); }
void write_text_inorder(char *p_text[], // Array of pointers to lines of input. int nlines ) // Number of input lines pointed to/referenced by array of pointers. //===================================== // This subroutine prints all input lines of text referenced by the "p_text" array of pointers. // Input lines are printed in the SAME order as they were read in. // A suitable heading is printed to indicate the nature of the output. // The report_message() routine is called to output an appropriate "MSG_INFO" message if there are no lines of text to print. {
// <*** your code here ***>
return; }
void write_text_reverse(char *p_text[], // Array of pointers to lines of input. int nlines ) {
// <*** your code here ***>
return; }
void report_message(char msg[MAX_MSG_NUM][MAX_MSG_LNG], // Message buffer enum msg_kind_type msg_kind ) // Message kind, i.e. kind of message: INFO, WARNING, or ERROR. //====================================================== { static char pname[] = "report_message"; // sub-program name.
register int i; static char info_prefix[] = "::> MSG: "; static char warn_prefix[] = "**> WRN: "; static char err_prefix[] = ">>> ERR: "; static char indent[] = " : ";
printf(NL); // Print a blank line to separate the message from previous output.
switch (msg_kind) { // Switch on "msg_kind" to determine which message prefix to output! case MSG_INFO: printf(info_prefix); break; case MSG_WARNING: printf(warn_prefix); break;
case MSG_ERROR: printf(err_prefix); break;
default: // Always check for an "un-handled" msg_kind! printf(">>>>>>: INTERNAL Error in function: %s ", pname ); printf(">>>>>>: Invalid msg_kind : [%d] ", msg_kind); break; }
msg[0][MAX_MSG_LNG -1] = cNUL;
printf("%s ", msg[0]); // Always print first message! for (int j=0; j < MAX_MSG_LNG; j++) msg[0][j] = cNUL; i = 1; while ( (i < MAX_MSG_NUM) && (*msg[i] != cNUL) ) {
msg[i][MAX_MSG_LNG -1] = cNUL;
printf("%s%s ", indent, msg[i]);
for (int j=0; j < MAX_MSG_LNG; j++) msg[i][j] = cNUL; i++; } return; }
text file:
Alex "M 41 74 170 120 jefferson st. Riverside NJ 8075, Bertm "M" 42 68 166 220 hobo Av. Phila PA 9119, Carl "M" 32 70 155 120 Jefferson St. Riverside NJ 8075, Dave "M" 39 72 167 7452 Terrace "At the Plaza" road SomeTown SD 91234, Elly F" 30 66 124 SomeTown SD 298, Fran "F" 33 66 115 9th, at Terrace plc Desert City CO 123, Gwen "F" 26 64 121 120 jefferson st. Riverside NJ 8075, Hank "M" 30 71 158 220 hobo Av. Phila PA 9119, Ivan "M" 53 72 175 120 Jefferson St. Riverside NJ 8075, Jake "M" 32 69 143 7452 Terrace "At the Plaza" road SomeTown SD 91234, Kate "F" 47 69 139 SomeTown SD 298, Luke "M" 34 72 163 9th, at Terrace plc Desert City CO 123, Myra "F" 23 62 98 120 jefferson st. Riverside NJ 8075, Neil "M" 6 75 160 220 hobo Av. Phila PA 9119, Omar "M" 38 70 145 120 Jefferson St. Riverside NJ 8075, Page "F" 31 67 135 7452 Terrace "At the Plaza" road SomeTown SD 91234, Quin "M" 29 71 176 SomeTown SD 298, Ruth "F" 28 65 131 9th, at Terrace plc Desert City CO 123.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started