I want to be able to go line by line through this text file and grab parts of it. For example,
Albert, Fred
4541231234
8888 Avenue Drive
Doe, John
6191231234
1234 State Street
Smith, Mike
8791231234
0987 Drive Avenue
I need to grab Albert and store it as the last name. Fred as the first name (excluding the ", ", and the phone number and address.
Searching through threads, I found a little help and this is what I have.
void AddressBook::readFile(Contact * phoneBook[])
{
std::string line, line1, line2, line3, line4;
std::ifstream myFile("fileName.txt");
std::string name, fName, lName, phoneNumber, address;
if (!myFile.is_open())
{
std::cout << "File failed to open." << std::endl;
return;
}
while (true)
{
if (!getline(myFile, line1))
{
break;
}
if (!getline(myFile, line2)) //need to parse into lName and fName
{
break;
}
if (!getline(myFile, line3))
{
break;
}
if (!getline(myFile, line4))
{
break;
}
addContact(line1, line2, line3, line4);
}
}
As you can see, this code only grabs the entire line. How can I stop at the comma, store it into a last name variable, and continue to the first name?