Reading file with delimiter - storing data to object

I am trying to read a file use the data line by line to create into an object.

The current file I have is like this and the code reading the file will be found below.

1223 Fake1 Name1 60 70 80 24 89 add1 Male
1224 Fake2 Name2 61 70 81 80 24 add2 Male
1225 Fake3 Name3 63 70 82 80 89 add3 Male
1226 Fake4 Name4 63 70 83 80 88 add4 Male


The problem I am having is that I need to put delimiters in the file so that a person can have more than one name and also the address can now hold multiple strings until the delimiter.

I would like to change my file to this;

1223 : Fake1 Name1 : 60 : 70 : 80 : 24 :89 : This will be address1 : Male
1224 : Fake2 Name2 : 61 : 70 : 81 : 80 :24 : This will be address2 : Male
1225 : Fake3 Name3 : 63 : 70 : 82 : 80 :89 : This will be address3 : Male
1226 : Fake4 Name4 : 63 : 70 : 83 : 80 :88 : This will be address4 : Male


How can I update the code below so that it can use the delimiters to create an object?

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
void loadFile(Person people[], int* i)
{
	ifstream infile("people2.txt");
	
	if ( !infile.is_open()) {
	  // The file could not be opened
		cout << "Error";
	}
	else
	{
		string str, str1, str2, str3, str4;
		int x[6];
		while(!infile.eof())
		{
			infile >> str1; 
		
			infile >> str2; 
			
			inile >> str; 
			x[0] = stoi(str);

			infile >> str; 
			x[1] = stoi(str);

			infile >> str; 
			x[2] = stoi(str);

			infile >> str; 
			x[3] = stoi(str);

			infile >> str; 
			x[4] = stoi(str);

			infile >> str; 
			x[5] = stoi(str);

			infile >> str3; 
			infile >> str4; 

			
			people[*i] = Person( x[0], str2 + " " + str1, x[1], x[2], x[3], x[4], x[5], str3, str4);
			(*i)++;
		}
		infile.close();
	}


}


A C library solution is finction strtok, see an example here:
http://www.cplusplus.com/reference/cstring/strtok/?kw=strtok

Or consider iterative use of std::string::find_first_of or similar functions:
http://www.cplusplus.com/reference/string/string/find_first_of/
Thank you for the response. I have tried implementing similar code but the problem is that it takes the whole file and stores it string by string. Its from there where I am actually struggling to only the values for the first line and storing that into an object before proceeding to the next lines.
I see, perhaps instead of using
infile >> str;
try
getline(infile, str);
This will give you a line at a time, instead of a word at a time.
I am still trying some of the codes from the links you have sent me but I am not sure I am going in the right direction.
Thanks Tipaye

This is the code I used in the end for anyone else trying to do the same thing.
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
#include <string>
#include <fstream>
#include <sstream>
#include <vector>


int main() {
  std::ifstream infile("people2.txt");
  if (!infile) {
    std::cerr << "Could not open file." << std::endl;
    return 1;
  }

  std::string line;
  while (std::getline(infile, line)) {  // Read file line by line.
    std::string field;
    std::vector<std::string> separated_fields;
    std::istringstream iss_line(line);
    while (std::getline(iss_line, field, ':')) { // Split line on the ':' character
      separated_fields.push_back(field);    // Vector containing each field, i.e. name, address...
    }
    // Do something with the results
    separated_fields[0]; // ID
    separated_fields[1]; // Names
    separated_fields[2]; // Number
    separated_fields[3]; // Number
    separated_fields[4]; // Number
    separated_fields[5]; // Number
    separated_fields[6]; // Number
    separated_fields[7]; // Address
    separated_fields[6]; // Sex 
  }
}
Topic archived. No new replies allowed.