Q:

how to extract out data from a txt file which is seperated by |

I am looking for a way to extract out data from a txt file which data is seperated by row and each column of data is seperaed by |
Here's an example
12|john bravo|123 kings street
15|marry jane|321 kings street

Previously i did it by separating using spaces like this
12 john kingstreet
15 marry kingstreet

But it poses a problem when I add a last name to the names/ add an address with spaces, ex: john bravo
So I decided to separate the column data using |
this is how I extract the data
struct PERSON{
   int id;
   string name;
   string address;
};

//extract
int main(){
   PERSON data[2];

   ifstream uFile("people.txt");
   int i = 0;
   while(uFile >> data[i].id >> data[i].name >> data[i].address){
    i++;
   }
   return 0;
}

So how do i extract if the columns are separated by | ??

A:

Use getline() twice:
First, get each line use default seperator (new line); second, for each segment from first step, use '|' as seperator. "stringstream" class may be used to transfer data.

@edward The code below is modified from yours, and I think @P0W58 's answer is better.
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

const int length = 2;

struct PERSON
{
    int id;
    string name;
    string address;
};

//extract
int main()
{
    PERSON data[length];

    ifstream fin("people.txt");

    int i = 0;
    while(true)
    {
        string segment;
        if (!getline(fin, segment))
            break;

        stringstream transporter;
        transporter << segment;

        string idString;
        getline(transporter, idString, '|');
        getline(transporter, data[i].name, '|');
        getline(transporter, data[i].address, '|');

        stringstream idStream;
        idStream << idString;

        idStream >> data[i].id;

        i++;
    }

    for (i=0; i<length; i++)
        cout << data[i].id << '+' << data[i].name << '+'\
            << data[i].address << endl;

    return 0;
}

A:

To read into a struct , I'd overload << and then parse the text as mentioned in one of the answer .
Something like this :
#include<sstream>
//...

struct PERSON{
   int id;
   std::string name;
   std::string address;

   friend std::istream& operator >>(std::istream& is, PERSON& p)
   {
     std::string s;
     std::getline(is, s); //Read Line, use '\r' if your file is saved on linux
     std::stringstream ss(s);
     std::getline(ss, s, '|');  //id
     p.id = std::atoi(s.c_str()); 
     std::getline(ss, p.name, '|'); // name
     std::getline(ss, p.address, '|'); //address

    return is ;
   }
};

And then you can probably do,
std::ifstream fin("input.txt");
PERSON p1;
while (fin >> p1)
   //std::cout << p1.id << p1.name << std::endl ; 

You can overload << too

