Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help in finding bugs in the code in the attached pictures above and explaination how to you the logs file and assertion to

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

I need help in finding bugs in the code in the attached pictures above and explaination how to you the logs file and assertion to find the bugs.

Here is the code in text format

Main.c

#define _CRT_SECURE_NO_WARNINGS #include  #include "stringhelp.h" #include  #include  int main(void) { char testStr[] = { "This is an string with embedded newline characters and n12345 numbers inside it as well 7890" }; struct StringIndex index = indexString(testStr); int i; printf("LINESn"); for (i = 0; i Stringhelp.h #pragma once #ifndef STRINGHELP_H #define STRINGHELP_H #define MAX_STRING_SIZE 511 #define MAX_INDEX_SIZE 100 #define MAX_WORD_SIZE 23 struct StringIndex { char str[MAX_STRING_SIZE + 1]; int wordStarts[MAX_INDEX_SIZE]; int lineStarts[MAX_INDEX_SIZE]; int numberStarts[MAX_INDEX_SIZE]; int numWords, numLines, numNumbers; }; /** * Return the index of the next whitespace. * @param str - the string to search * @returns the index of the next white space or the position of the string terminator. */ int nextWhite(const char* str); /** * Return true if the string contains only digits. * @param str - the string to check * @param len - the number of characters to check * @returns true if the string is only digits. */ int isNumber(const char* str, const int len); /** * Build an index for a string showing the start of the lines, * words, and numbers in the string. * @param str - the string to search * @returns the index of the next white space or -1 if there is none. */ struct StringIndex indexString(const char* str); /** * Return the number of lines in the index. * @param idx - the index to get the number of lines in it * @returns the number of lines in the index */ int getNumberLines(const struct StringIndex* idx); /** * Return the number of words in the index. * @param idx - the index to get the number of words in it * @returns the number of words in the index */ int getNumberWords(const struct StringIndex* idx); /** * Return the number of numbers in the index. * @param idx - the index to get the number of numbers in it * @returns the number of numbers in the index */ int getNumberNumbers(const struct StringIndex* idx); /** * Return the nth word from the index * @param word - where the result will be placed * @param idx - the index to use * @param wordNum - the index of the word to retrieve * @returns the word or an empty string if index is invalid */ void getWord(char word[], const struct StringIndex* idx, int wordNum); /** * Return the nth number from the index * @param word - where the result will be placed * @param idx - the index to use * @param wordNum - the index of the number to retrieve * @returns the number or an empty string if index is invalid */ void getNumber(char word[], const struct StringIndex* idx, int numberNum); /** * Return a pointer to the start of a line * @param idx - the index to use * @param lineNum - the index of the line to retrieve * @returns a pointer to the start of the line */ char* getLine(struct StringIndex* idx, int lineNum); /** * Prints characters until the terminator is found. * @param s - the string to print * @param start - the index to start printing * @param terminator - the character to stop printing at when encountered. */ void printUntil(const char* s, const int start, const char terminator); /** * Prints characters until a space is found. * @param s - the string to print * @param start - the index to start printing */ void printUntilSpace(const char* s, const int start); #endif Stringhelp.c #define _CRT_SECURE_NO_WARNINGS #include "stringhelp.h" #include  #include  #include  int nextWhite(const char* str) { int i, result = -1; for (i = 0; result numLines; } int getNumberWords(const struct StringIndex* idx) { return idx->numWords; } int getNumberNumbers(const struct StringIndex* idx) { return idx->numNumbers; } void getWord(char word[], const struct StringIndex* idx, int wordNum) { int i, sp, start; word[0] = ''; if (wordNum numWords && wordNum >= 0) { start = idx->wordStarts[wordNum]; sp = nextWhite(idx->str + start); for (i = 0; i str[start + i]; } word[sp] = ''; } } void getNumber(char word[], const struct StringIndex* idx, int numberNum) { int i, sp, start; word[0] = ''; if (numberNum numNumbers && numberNum >= 0) { start = idx->numberStarts[numberNum]; sp = nextWhite(idx->str + start); for (i = 0; i str[sp + i]; } word[sp] = ''; } } char* getLine(struct StringIndex* idx, int lineNum) { char* result = NULL; if (lineNum numLines && lineNum >= 0) { result = idx->str + idx->lineStarts[lineNum]; } return result; } void printUntil(const char* s, const int start, const char terminator) { int i; for (i = start; s[i] != '' && s[i] != terminator; i++) printf("%c", s[i]); } void printUntilSpace(const char* s, const int start) { int i; for (i = start; s[i] != '' && !isspace(s[i]); i++) printf("%c", s[i]); }

The log4c.h to be used is below:- #pragma once #ifndef LOG4C_H #define LOG4C_H #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #define _CRT_SECURE_NO_WARNINGS #include  /* Log message severity codes*/ #define L4C_ERROR 1 #define L4C_WARNING 2 #define L4C_INFO 3 /* This is the size to use for the error messages returned from l4cCheck() */ #define L4C_ERROR_MSG_MAX 50 /* * You should store the log file in a structure of this type. * Eg. struct Log4CStruct logFile = l4cOpen("log.txt", 0); */ struct Log4cStruct { FILE* logFile; char fileStatus; char enabled; char autoFlush; char filter; }; /** * Open a log file and return a structure containing information about the log file. * You must open a log file before it can be used. * You should call l4cCheck() after opening the file to make sure that the file opened correctly. * @param fileName - the name of the file to open as a null terminated string. * @param append - a Boolean value which if false, will overwrite the file if it exists. If true, it will append * to the existing file. * @returns A struct identifying the log file. */ struct Log4cStruct l4cOpen(char fileName[], const int append); /** * This will close an open log file. Once the file is closed, it cannot be used again unless re-opened. * @param log - the struct representing the log file to close. */ void l4cClose(struct Log4cStruct* log); /** * Write an error message to the log file. * @param log - the struct representing the log file to write to. * @param msg - the text string to write to the log file. */ void l4cError(struct Log4cStruct* log, const char msg[]); /** * Write a warning message to the log file. * @param log - the struct representing the log file to write to. * @param msg - the text string to write to the log file. */ void l4cWarning(struct Log4cStruct* log, const char msg[]); /** * Write an information message to the log file. * @param log - the struct representing the log file to write to. * @param msg - the text string to write to the log file. */ void l4cInfo(struct Log4cStruct* log, const char msg[]); /** * Write a formatted message to the log file. * @param log - the struct representing the log file to write to. * @param severity - The severity of the message using L4C_ERROR, L4C_WARNING or L4C_INFO. * @param format - a format string, just like you would use for printf. * @param ... - a varying number of arguments that will provide values to be used in the format string. * This works just like printf(format, ...). */ void l4cPrintf(struct Log4cStruct* log, const int severity, const char format[], ...); /** * This checks the status of the log file and returns 0 if it is function normally. * @param log - the struct representing the log file to check. * @param errMsg - a string into which an error message will be placed. This string must be at least * L4C_ERROR_MSG_MAX + 1 characters in length. If there is no error, it will contain an empty string. * @returns 0 if the file is functioning normally and non-zero if there is an error. If there is an * error, stop using the log file and print out the error message. */ int l4cCheck(const struct Log4cStruct* log, char errMsg[]); /** * Temporarily disable the log file without closing it. While disabled, writes will have no effect. * @param log - the struct representing the log file to disable. If a NULL parameter is passed, the * function will have no effect. */ void l4cDisable(struct Log4cStruct* log); /** * Enable the log file without closing it. This will resume writing to the file if it was disabled. * @param log - the struct representing the log file to ensable. If a NULL parameter is passed, the * function will have no effect. */ void l4cEnable(struct Log4cStruct* log); /** * Determine if the log file is enabled and the messages will be written to the file. * @param log - the struct representing the log file to query. * @returns true if the file is enabled. If a NULL log file is passed, a negative value is returned. */ int l4cIsEnabled(struct Log4cStruct* log); /** * This will filter the log file so that only messages of the indicated severity or higher will * be written to the log file. If you set it to L4C_ERROR, you will only see errors messages in * the log file. Setting it to L4C_INFO will send all messages to the log file. L4C_INFO is the * default setting when a log file is opened. * @param log - the struct representing the log file to query. If NULL, the function has no effect. * @param severity - the lowest severity to be sent to the log file. * @returns the previous setting of the filter. If log was NULL, returns a negative value. */ int l4cFilter(struct Log4cStruct* log, const int severity); /** * This will return the current setting of the log filter. * @param log - the struct representing the log file to query. * @returns the current setting on the log severity filter. * Returns filter setting or negative value if log was NULL. */ int l4cGetFilter(const struct Log4cStruct* log); /** * Auto flush will flush a message to disk right after it has been written to the log file. * This virtually guarantees all your messages will appear in the log file, even if your * program crashes. The downside is that this has a major impact on performance if you are * writing many log messages. The default is to have auto flush on. This function will disable * auto flush if it is on. If the log is NULL, it has no effect. * @param log - the struct representing the log file to query. */ void l4cDisableAutoFlush(struct Log4cStruct* log); /** * Auto flush will flush a message to disk right after it has been written to the log file. * This virtually guarantees all your messages will appear in the log file, even if your * program crashes. The downside is that this has a major impact on performance if you are * writing many log messages. The default is to have auto flush on. This function will enable * auto flush if it is off. If the log is NULL, it has no effect. * @param log - the struct representing the log file to query. */ void l4cEnableAutoFlush(struct Log4cStruct* log); /** * Determine if auto flush is enabled for the log file. * @param log - the struct representing the log file to query. * @returns true if auto flush is enabled (default) or false if not enabled. * Returns negative value if log is NULL. */ int l4cIsAutoFlush(const struct Log4cStruct* log); #endif#pragma once

SFT221 - Workshop 6 Learning Outcomes - Learn to use a logger to debug code, - Learn to use assertions to aid in debugging, - Learn debugging techniques. Instructions It just gets worse and worse. We let a junior developer work on the code you just fixed and it is broken again! This time, we want you to use a log file and assertions to find the bugs and fix them. DO NOT use the debugger as we want you to compare using log files and assertions to using the debugger. Take notes as to which debugging tools you use as there will be questions about which ones you used to find each bug later. You should use the log4c library that was discussed in class and add log sta tements to the code to help you debug the code. You should also insert assertions to detect impossible situations and situa tions that indicate that something is wrong with the code. These assertions should be spread a cross all the stringhelp functions to detect possible errors, even after debugging is complete. You should consider that the assertions are being added for the long-term life of the program, not just for this debugging session. With luck, the assertions you add today will detect bugs introduced by other junior developers. stringhelp.h *pragma once \#ifndef STRINGHELP_H Hdefine STRINGHELP_H \#define MAX_STRING_SIZE 511 \#define MAX_INDEX_SIZE 100 \#define MAX_WORD_SIZE 23 struct StringIndex \{ char str[MAX_STRING_SIZE + 1]; int wordStarts[MAX_INDEX_SIZE]; int lineStarts [MAX_INDEX_SIZE]; int numberStarts[MAX_INDEX_SIZE]; int numbords, numLines, numNumbers; 3; /v * Return the index of the next whitespace. * eparan str - the string to search * Greturns the index of the next white space or the position of the string terminator. */ int nextldhite(const char* str); * Return true if the string contains only digits. * eparan str - the string to check * Qparam len - the number of characters to check * areturns true if the string is only digits. */ int isNumber(const char* str, const int len); * Build an index for a string showing the start of the lines, * words, and numbers in the string. * Qparam str - the string to search * Qreturns the index of the next white space or 1 if there is none. / struct StringIndex indexString(const char* str); / * Return the number of lines in the index. * Qparam idx - the index to get the number of lines in it * areturns the number of lines in the index / int getNumberLines(const struct StringIndex* idx); / * Return the number of wards in the index. * Qparam idx - the index to get the number of words in it * areturns the number of words in the index / int getNumberwords(canst struct StringIndex* idx); /+ * Return the number of numbers in the index. * aparam idx - the index to get the number of numbers in it * ereturns the number of numbers in the index / int getNumberNumbers(const struct StringIndex* idx); /4 * Return the nth word from the index * aparan word - where the result will be placed * eparam idx - the index to use * Qparam wordNum - the index of the word to retrieve * areturns the word ar an enpty string if index is invalid / void getword(char word[], canst struct StringIndex* idx, int wordNum); /v * Return the nth number from the index * Eparan word - where the result will be placed * eparam idx - the index to use * Qparam wordNum - the index of the number to retrieve * ereturns the number or an empty string if index is invalid */ void getNumber(char word[], const struct StringIndex* idx, int numberNum); /kx * Return a pointer to the start of a line * Qparam idx - the index to use * Qparam lineNum - the index of the line to retrieve * Qreturns a pointer to the start of the line / char* getLine(struct StringIndex* idx, int lineNum); / * Prints characters until the terminator is found. * Qparam 5 - the string to print * Qparam start - the index to start printing * Qparam terminator - the character to stop printing at when encountered. / void printUntil(const char*s, const int start, const char terminator); / * Prints characters until a space is found. * Qparam 5 - the string to print * aparan start - the index to start printing / void printUntilSpace(const char* s, const int start); Hendif stringhelp.c \#define_CRT_SECURE_NO_WARNINGS \#inc lude "stringhelp.h" int nextWhite(const char* str) \{ Deliverables DueDate: This workshop is due at 23:59 at the end of yourlab day. Late workshops cannot be accepted. You should submit: - A zipped Visual Studio project that contains the debugged, working version of the code that a includes your use of the log file and assertions, - The log file you produced to help in the debugging process, - A document which lists: The line(s) containing each bug, - The corrected version of the line(s), c What was wrong with the line(s) and how you fixed it, \& The debugger tool or technique you used to recognize and find the bug. - A document called reflect.txt which answers the reflections below

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions