Answered step by step
Verified Expert Solution
Question
1 Approved Answer
bool isFileOpenForRead (ifstream& ifs, const string filename) ifs - the input stream representing the file resource filename - the name of the file to open
bool isFileOpenForRead (ifstream& ifs, const string filename) ifs - the input stream representing the file resource filename - the name of the file to open for reading returns - true if stream is open successfully, false otherwise. int readArrayFromFile(ifstream& ifs, string list[]) ifs - the open stream to read from list - the array to load with values read in returns - the number of elements read into the array Reads a CSV file consisting of integer values followed by a word, each comma delimited and newline terminated. It loads the array with a composite string values made from concatenating the numeric value and word together, e.g. -Apple. Hint: Use the version of the getline(stream, string, delimiter), where the delimiter is a character representing the character the getline() is to read up to. So, for the string , Apple, use a comma, for the delimiter to read the o. If you don't specify the delimiter it defaults to the newline , which allows you to read to the end of the line. Example: If the input file contains the values: 0, Apple 1, Banana 2, Cantelope 3, Date 4, Eggplant 5, Fig The array list will contain the elements "O-Apple", "1-Banana", "2-Cantelope", "3-Date", "4-Eggplant", "5-Fig". bool isFileOpenForRead(ifstream& ifs, const string filename) { ifs.open(filename); return ifs.is_open(); } int readArrayFromFile(ifstream& ifs, string list[]){ int counter = 0; string tempString; string tempNum; while (!ifs.eof()) { getline(ifs, tempNum, ','); getline(ifs, tempString); list[counter] = tempNum tempNum + "-" + tempString; counter += 1; } return counter; }
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