Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C++ Ignoring New Lines while Reading Files I have trouble with the 2 following files and IMPORTANT NOTE: The Ns are New Lines, they are
C++ Ignoring New Lines while Reading Files
I have trouble with the 2 following files
and
IMPORTANT NOTE: The "N"s are New Lines, they are NOT in the file and only serve as a visual.
Problem: My function to read a file does not appear to work when there are multiple newlines or if newlines separate a record I should add to a linked list. Each file ends in at least one new line, but as shown in the first file, it will not work if there's another new line after it.
My Read File Function:
{id: 1234567, first: Mary, last: Green, DOB: 1996-10-03,GPA: 4.0) {id: 1234568, first:Peter, last:White, DOB: 1997-05-22,GPA: 3.8} {id: 1654238, first:Nick, last:Park,DOB: 1995-08-18, GPA:4.0) {id: 1234587, first:Katy, last: Green, DOB: 1995-08-18, GPA: 4.0} {id: 1234567, first: Mary, last: Green, DOB: 1996-10-03, GPA: 4.0} {id: 1234568, first:Peter, last: White, DOB: 1997-05-22, GPA: 3.8} {id: 1654238, first: Nick, last:Park, DOB: 1995-08-18, GPA:4.0} {id: 1234587, first:Katy, last: Green, DOB: 1995-08-18, GPA: 4.0) {id: 1654238, first: Nick, last:Park, DOB: 1995-08-18, GPA:4.0} {id: 1654238, first:Nick, last:Park,DOB: 1995-08-18, GPA:4.0} bool LinkedList::readFile(string filename) { ifstream ifs(filename); string str; while (getline(ifs, str)) { str.erase (remove(str.begin(), str.end(), ' '), str.end(); str.erase(remove(str.begin(), str.end(), ' '), str.end(); if (str.find("delete") != string: :npos) { int loc = str.find("delete") + 6; int loc2 = str.find(" "); loc++; int key = stoi(str.substr(loc, loc2 - loc)); deleteItem(key); else if (str.find("del") != string::npos) { int loc = str.find("del") + 3; int loc2 = str.find(' '); loc++; int key = stoi(str.substr(loc, loc2 - loc)); deleteItem(key); else { additem(str); return trueStep 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