Program isn't outputting a text file's parsed info

Good afternoon!
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.

CODE:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main()
{
const int size = 20;
string line;
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 gaharrison@gmail.com 2503 N Main Street, Baytown, TX ";

cout << "Opening file ClientList.txt : " << endl;

ifstream inputfile;
inputfile.open("ClientList.txt");

if(!inputfile.is_open())
{
cout << "Could not open the file." << endl;
return 1;
}



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


for(i = 0; i < clientcount; i++) // display the currect info
{
cout << left << setw(22) << name[i];
cout << setw(14) << phone_number[i];
cout << setw(22) << email[i];
cout << setw(22) << street[i];
cout << cityState[i] << endl;
}



// Modify a phone number and address
phone_number[3] = "555-555-5555";
street[5] = "555 Elm Street,";

// Get input for 2 addition records -- remember to increment clientcount for each additional record

for(i = 0; i < clientcount; i++) // display the currect info
{
cout << left << setw(22) << name[i];
cout << setw(14) << phone_number[i];
cout << setw(22) << email[i];
cout << setw(22) << street[i];
cout << cityState[i] << endl;
}

ofstream outputfile;
outputfile.open("ClientListv2.txt");

if(!outputfile.is_open())
{
cout << "Error opening the output file." << endl;
return 1;
}

{
outputfile << left
<< setw(22) << name[clientcount]
<< setw(14) << phone_number[clientcount]
<< setw(22) << email[clientcount]
<< setw(22) << street[clientcount]
<< cityState[clientcount] << endl;
}

outputfile.close();



return 0;
}



Last edited on
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:

http://www.cplusplus.com/reference/string/string/find/
http://www.cplusplus.com/reference/string/string/rfind/
Perhaps something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

struct Person {
	std::string name;
	std::string phone;
	std::string email;
	std::string street;
	std::string cityState;
};

std::istream& operator>>(std::istream& is, Person& per)
{
	std::string line;

	if (std::getline(is, line)) {
		const auto p {line.find_first_of("0123456789")};
		const auto t {line.find(' ', p)};
		const auto e {line.find(' ', t + 1)};
		const auto s {line.find(',', e + 1)};

		per.name = line.substr(0, p - 1);
		per.phone = line.substr(p, t - p);
		per.email = line.substr(t + 1, e - t - 1);
		per.street = line.substr(e + 1, s - e - 1);
		per.cityState = line.substr(s + 2);
	}

	return is;
}

std::ostream& operator<<(std::ostream& os, const Person& per)
{
	return os << std::left << std::setw(22) << per.name << std::setw(14) << per.phone
		<< std::setw(22) << per.email << std::setw(22) << per.street << per.cityState;
}

int main()
{
	const size_t size {20};
	size_t clientcount {}; // loop variable and actual number of records

	//const string example {"George A Harrison 713-555-1234 gaharrison@gmail.com 2503 N Main Street, Baytown, TX "};

	std::cout << "Opening file ClientList.txt\n";

	std::ifstream inputfile ("ClientList.txt");

	if (!inputfile) {
		std::cout << "Could not open the file.\n";
		return 1;
	}

	Person people[size];

	for (Person per; clientcount < size && inputfile >> per; people[clientcount++] = per);

	for (size_t i = 0; i < clientcount; ++i)
		std::cout << people[i] << '\n';

	// Modify a phone number and address
	//phone_number[3] = "555-555-5555";
	//street[5] = "555 Elm Street,";

	// Get input for 2 addition records -- remember to increment clientcount for each additional record

	std::ofstream outputfile ("ClientListv2.txt");

	if (!outputfile.is_open()) {
		std::cout << "Error opening the output file.\n";
		return 1;
	}

	for (size_t i = 0; i < clientcount; ++i)
		outputfile << people[i] << '\n';
}

Topic archived. No new replies allowed.