fstream: Saving parts of a line into different strings without the white spaces included ?

Lets say I have a text file with a bunch of names on it.

The position where each element start remains constant throughout the lines(example, last name starts after 25 spaces).
I want to save each name into a respective string. like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include  <fstream>
using namespace std;

int main()
{
ifstream inFile;
string aLine;
string firstName, middleName, lastName, maidenName; 

inFile.open("some file");
	if (inFile.fail())
	{
		cerr << "Error" << endl;
	}
	getline(inFile, aLine);

firstName = aLine.substr(1 - 1, 25);
middleName = aLine.substr(26 - 1, 25);
lastName = aLine.substr(51 - 1, 50)
maidenName = aLine.substr(101 - 1, 50);

return 0; 
}


How do I do this without copying the white spaces into the strings ? Again it's a file with a bunch of names on it.
Last edited on
it'd be helpful to share at least a couple of lines from the text file as well
Here. Ignore the numbers at the end for now.

1
2
3
Carlos                   Miguel                   Rodriguez                                         Soto                                              01262015015300015560078739922347872342345                                                           
Felix                    Gabriel                  Perez                                             Roman                                             01192015009020509401478723456437873451234                                                           
David                    Piere                    Soto                                              Colon                                             12232014111340912410578735623453054562345                                                           
Last edited on
You don't need to substring the line into first, last etc names in this case but by defining a Person struct you can read off the data directly into the data-members of the object and save them in a std::vector<Person>. This, of course, assumes that the file is uniform in that each person has all the attributes, no missing data or other irregularities.
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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

struct Person
{
    std::string m_firstName;
    std::string m_middleName;
    std::string m_lastName;
    std::string m_maidenName;

    Person(){}
    Person(const std::string& fName, const std::string& midName, const std::string& lName, const std::string& mName)
    : m_firstName(fName), m_middleName(midName), m_lastName(lName), m_maidenName(mName){}
};
std::ostream& operator << (std::ostream& os, const Person& p)
{
    os << p.m_firstName << " " << p.m_middleName << " " << p.m_lastName << " " << p.m_maidenName << '\n';
    return os;
}
int main()
{
    std::ifstream inFile("F:\\test.txt");
    std::vector<Person> persons{};
    if(inFile)
    {
        Person p{};
        while (inFile >> p.m_firstName >> p.m_middleName >> p.m_lastName >> p.m_maidenName)
        {
            if(inFile)
            {
                persons.push_back(p);
            }
        }
    }
    for (const auto& elem : persons)
    {
        std::cout << elem ;
    }
/*TEXT FILE
Carlos                   Miguel                   Rodriguez                       Soto    
Felix                    Gabriel                  Perez                               Roman                  
David                    Piere                    Soto                                Colon  
PROGRAM OUTPUT
Carlos Miguel Rodriguez Soto
Felix Gabriel Perez Roman
David Piere Soto Colon*/

ps: also ignoring the numbers at the end of each row as mentioned but if they are present in a file and not required they can be read into a dummy variable and discarded


Last edited on
Topic archived. No new replies allowed.