Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

no chatgpt or ai to solve the question below please, and clearly label which section you're doing (A2b or A2a) A2_cStringString_F24 A review of BIT

no chatgpt or ai to solve the question below please, and clearly label which section you're doing (A2b or A2a)

A2_cStringString_F24 A review of BIT 1400 and then objects. Assignment Deliverable This assignment has actually two deliverables: using c-strings and C syntax exclusively (A2a) and using the string class and C++ versions of these functions (A2b). In both A2a and A2b you will 1. read in a text file line by line. 2. Programmatically remove all block comments in the text 3. Bonus: removing all line comments from the text. 4. output the (comment free) contents of the text file to the screen. Learning Objectives We are reviewing file I/O in this assignment. After completing it: You will be able to use standard objects and the string class specifically. You will be able to compare and contrast C and C++ functions that provide similar functionality. See the appendix for a list of useful functions. You will invariably get experience looking up how to use some functions online. This is expected. Learning how to call a function is not considered cheating. See the appendix for useful string functions. You will also demonstrate proficiency in parsing c-string and string object text using pre-defined C and C++ functions. Related Lecture Materials Module 3 comparing character strings (from BIT 1400) and the string class. Tutorial/lab 3 should also be useful. A2b looks at C++ specific functions such as: getline, ifstreams, and the string class: https://stackoverflow.com/questions/9711977/using-the- fstream-getline-function-inside-a-class This may involve some looking around online for how to call the functions but the above example shows most such functions in action. Notice the question written in the above post uses an interesting function: eof() (or end of file). Getline *should* return a pointer to the string or NULL (thus you just test "if (getline(file,str)){...}" ) but I wanted to point out the eof call just in case the getline call doesn't work as expected. You don't want an infinite loop after all. Instructions This assignment continues the work similarly to A1 but this time, asks you to read in a text file twice using c-strings and string objects respectively (A2a and A2b). a) A2a: read in a text file line by line to remove all block comments in the file (indicated by /* */ just like C comments). You then output the revised (comment free) text to the screen using printf. You may only use pure C syntax (so cout, fileopen and getline are not allowed). b) A2b: This time you will only use C++ relates string functions and streams (like ifstreams). You can't use c-string functions and functions like fgets. c) You will be asked to test your code by ensuring the text produced in A2a and a2b are the same d) Bonus: for both A2a and A2b there is a 5% bonus for removing both line comments (//) and block comments ( /* */). This is more complicated than just removing text on a line after a "//". For example " What // /* Do you */ do here?"....it should be "What " for all the text on the line. The line comment would remove the block comment start and end. "What /* Do // you do */ here?" should become "What here?" since the block comment starts before the line comment. The text file that was used can be found here: https://brightspace.carleton.ca/d2l/le/content/292529/viewContent/3911875/View TO UPDATE The solution for A2a will be provided after the due date to help you with A2b. You can just copy and modify that code if you like. Warm Up Tasks A2a make a function removeSubstring that takes two char pointers p_start and p_end. Both pointers point to the same character array. removeSubstring removes characters between p_start and p_end (excluding the character at p_end D e m o ' ' c h a r '\0' P_start P_end A call to removeSubstring with the above pointers would result in the following char array: D e a r '\0' c h a r '\0' You just need to copy characters p_end and later over to p_start. You can also use functions like memcpy if you like (and are feeling brave). Hint: I had a brief bug when writing the solution. I didn't copy the '\0' at the end because I was using strlen to figure out how many characters were after p_end. Hint 2: This function may be useful later in the assignment. A2b do a function removeSubstringCPP that takes a string, a start index and an end index and returns a string with the text between the start and end removed. Notice this is not the same as removeSubstring from A2a. The number of characters removed is the difference of the length between the original string and the string returned from removeSubstringCPP File Reading Now for the main task. You will read in a text file, remove comments from the text, and output the revised text (to the screen) We will read ALL the text from the file, save it to a single large string or character array, and then remove all comments from that string or c-string. 1. Make sure your name is at the top of the file in a comment. 2. The functions removeCommentsC and removeCommentsCPP should parse the input text file, output (using printf for A2a and cout for A2b) the revised lines of the text and return the number of characters removed from the file's text. These are the two major functions. You should break the problem into smaller functions. I now suggest you read in the entire file as a single c-string or string. Then remove the comments and return the number of commented out characters removed. A2a: Perhaps a function that takes a character array as input and edits that array to remove all comments would be helpful. That function would return the number of characters removed. A2b: A function that takes a string to edit and returns a revised string with no comments would be useful. The number of characters removed would be the difference in those string lengths. Could the functions you made for the warm-up be useful if you find where a "/*" and "*/" are located? A line comment would be between "//" and " " (the end of the line). Notice removeCommentsC should only use C functions and removeCommentsCPP should use new C++ functions like getline, and ifstream's open() Figure 1 A sample output of the program (A2a and A2b) when run using the provided text file. Notice which text was removed. 3. Make sure you test your code sufficiently that you are confident the old and new file parsing functions work the same. Function strcmp and string function compare might be useful. You might want to look at string functions (A2b) and c-string related functions like strstr and strlen as well. There is no real rhyme or reason to the given text file but general C syntax is being used. When the text has a "/*", all text after that until a "*/" will be considered a comment....even if it spans multiple lines and includes other "/*" and "//" text. Testing All text on a line after "//" will be commented out (this is the bonus question). You may assume (but don't need to) that there are a maximum of 200 characters on a line and no more than 100 lines in the file. Remember, when reading in a text file you need to check if the file was opened successfully. If the file pointer is NULL after fopen is called, the file wasn't opened correctly. You should handle that scenario. Actually, your first task is to make sure the file is being read in correctly. Put the input text files in the same directory as your code (in windows, not in visual studio). Work Around: If you can't get the file input working as expected, just make an array of text that looks the same as the file. Notes: Plan on paper then implement. If you code without a plan, it will take MUCH longer. Do the code a bit at a time and test at each step. If you do it all at once debugging becomes overwhelming.....compiling is not the same as testing your code. The task can be broken down (and coded) in 3 parts: o Open and read the file line by line. We will save all the text into one big array or string and then remove the comments. o Output how many characters were removed by your program and the revised text. Work through a number of examples yourself to understand the problem. Do not code until you have a very clear plan and you think you can handle all scenarios. For example, what if the text looks like this? "This is /* a line of Text spanning multiple lines. // You May think this example is */ stupid /* but it should work*/. For line comments, once a line comment starts all text after and including the "//" is removed. If you are confused, please ask clarification questions on the Brightspace discussion board (don't give your code or give away answers). This requires you to start working early. After you read in all the text to a single array, confirm it was done correctly. Then remove comments by removing text between /* and */ (and optionally between // and ). Notice, if the file was huge, moving this many characters would be slow but we aren't going to worry about that. Questions and instructions can seem obvious to me but make no sense to students and everyone is confused until someone asks. Please ask. Submitting Assignment A2a is worth of what a typical assignment is. A2b is another half. Together they count as 1 assignment mark. Files should be named: A2a__cString_F24.cpp and A2b__StringObj_F24.cpp is once again the last 4 numbers of your Carleton ID. Make sure you code compiles and runs before submitting. If you can't figure out a part, comment out your code to get lots of partial credit. Figure out a smart way to work around the missing code (like making an array of text rather than reading the file. Submit your .cpp file (and ONLY that) to Brightspace's Assignment 2a for part a 1 and to Assignment 2b for part b. Remember, there are no late extensions and late assignments are marked as 0. You can also submit multiple times and we grade the last one so SUBMIT EARLY, SUBMIT OFTEN. Hence there is no reason for a late submission. Appendix C Functions Useful C functions to look at (https://www.tutorialspoint.com/c_standard_library/stdio_h.htm) fscanf: read the next set of characters up to a white space from a file stream and save it into a char array. This is almost identical to scanf except you pass in a file as a parameter. You can use fscanf_s or fscanf depending on what is easier for you. If using Windows, I suggest fscanf_s since it is safer. Note: fscanf, although useful, isn't useful for this assignment since you will lose all the white space in the file. fgets: Read a LINE of characters up to a new line symbol (' ') from a file stream. To get all the lines of a file into a single array you can keep calling fgets until it returns NULL (the end of the file is reached). This is demonstrated in the code below. Notice we assume no line is longer than 100 characters long. p_buffPos is just pointing to places in array a_textBuff where the next line of text should go: char* p_buffPos = a_textBuff; while (fgets(p_buffPos, 100, p_file) != NULL) { int lineLen = strlen(p_buffPos); p_buffPos = a_textBuff + lineLen; } fread: Read a specific number of values from a file or until the end of file is reached. Here is an example of the function in action: size_t numChars = fread(a_textBuff, sizeof(char), 50, p_file); a_textBuff[numChars] = '\0'; This copies 50 characters over from p_file to a_textBuff BUT does not put a terminating '\0' at the end of a_textBuff. fopen / fopen_s: open a file stream based on a file name. Note that you should ALWAYS check to see if the file was opened successfully. FILE* p_outFile; fopen_s(&p_outFile, "TestFile.txt", "r"); // make to file TestFile.txt using stream p_outFile fclose: close an opened file stream when you are finished with it. C++ String Functions**************** Cin: The equivalent to scanf and fscanf, data comes from a source and is send to a variable. Like fscanf, this isn't useful for this assignment but you still want to practice using it. There is something strange about C++ file streams. There is no easy way to accurately get the number of characters in the file (there are lots of online examples using the same approach, but they are all off by a little bit). To read in an entire file and store it as a string, here are 3 approaches (all acceptable): 1. Read the file line by line and append the text from each line to your final string. Make sure you don't lose characters or you will have to add them back into your string. 2. Assume the file has < 1000 characters, call the ifstream read function with 1000 as the file size. The function read a character array rather than a string so just make a string based on the character array afterwards: string myText(a_textbuffer); 3. Calculate the number of characters correctly using the function below. This will mean the array being written to is the minimum size. This is a hacky work around but it works https://stackoverflow.com/questions/22984956/tellg-function-give-wrong-size-of- file/22986486#22986486 size_t getFileSize(string txtFile) { ifstream inFile; const size_t BUFF_SIZE = 500; size_t numRead = 0; inFile.open(txtFile, ios::in); if (inFile.is_open()) { char readBuffer[BUFF_SIZE + 1]; inFile.seekg(0, std::ios::beg); while (inFile.read(readBuffer + numRead, BUFF_SIZE) || inFile.gcount() != 0) { numRead += inFile.gcount(); } inFile.seekg(0, std::ios::beg); inFile.close(); } return numRead; } Getline Fileopen read Cout: Output information to a file stream or the screen. These functions include cin, cout, getline, and fileopen (C++) compared to scanf, printf, fgets, and fopen (C).

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

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

How do "school voucher programs" threaten public-sector employees?

Answered: 1 week ago

Question

Define Marketing research.

Answered: 1 week ago

Question

1. What is blood circulation? 2. Three types of blood vessels?

Answered: 1 week ago

Question

Why are red blood cells Red in colour?

Answered: 1 week ago