I am attempting to read information from a file using getline() but the information is not outputting correctly. I believe there may be an issue with the way I am using substrings to parse out the data within the text file. Within the code I've created a string that provides an example of how a line looks within the text file. There are 10 other lines similar to this string.
What I've attempted in xcode:
- read the "ClientList.txt" file
- display the parsed out information using substrings
- output a new file "ClientListv2.txt" file
Believe I'm failing to:
- properly use substrings
- properly use arrays
- properly output the information I've gathered
- have the ClientListv2.txt file display any information.
using namespace std;
const int size = 20;
string name[size], phone_number[size], email[size], street[size], cityState[size];
int i; // variable loop
int clientcount = 0; // loop variable and actual number of records
string example = "George A Harrison 713-555-1234 firstname.lastname@example.org 2503 N Main Street, Baytown, TX ";
cout << "Could not open the file." << endl;
while(!inputfile) // loop to read file info
getline(inputfile, line); // get 1 line of data from the text file
// parse out the data line into our data arrays info
name[clientcount] = line.substr(0, 22);
phone_number[clientcount] = line.substr(23, 12);
email[clientcount] = line.substr(38, 22);
street[clientcount] = line.substr(60, 21);
cityState[clientcount] = line.substr(82);
clientcount++; // increment clientcount
inputfile.close(); // done with input file so close the file
I believe there may be an issue with the way I am using substrings to parse out the data within the text file.
Yes, none of the entries have that particular size.
Normaly you have a delimiter such as whitespace or comma to separate the entries. In this case there isn't or a mix of it. That makes the thing complicated.
I suggest that you are searching for the @. Then you have the email.
Right of the email (after the space) the values (street, town, state) are comma separated.
Left of the email (before the space) is the phone number which contains no whitespaces and from the position before the phone number you have the name.
To find the spaces (and commas) you can use the find(...) and rfind(...) function of the std::string: