Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Problem to parse words from a text file that contains pipe as delimiter

I have a file that have values like
============================================================
Id | Name | Age |D | Type |P       |
   |      |     |O |      |H       |
   |      |     |B |      |Number  |
============================================================

I have to parse these lines and extract the column names which are Id,Name,Age,DOB,Type,PHNumber into a vector.

I am not able to get DOB and PHNumber names.
Please suggest how should I parse these lines.

What I have tried:

boost::regex col_check("^[A-Z].*");
while(getline(ifile,record))
    {
	   if(regex_match(record,col_check) )
		{
		    string tmp=record;
		    istringstream line(tmp);
		    while(getline(line,token,'|'))
		    {
		      rows.push_back(token);
		      
		      
		    }
	}
}
Posted
Updated 3-Nov-16 5:38am
v3
Comments
Richard MacCutchan 21-Sep-16 10:35am    
DOB and PHNumber are spread over more than one line, so you would need to read all the header lines and concatenate the different pieces first.
[no name] 21-Sep-16 11:34am    
http://www.cplusplus.com/reference/cstring/strtok/
[no name] 21-Sep-16 21:39pm    
Who created this error prone file schema? If you then perhaps learn about csv files: https://en.wikipedia.org/wiki/Comma-separated_values first and adapt accordingly.
Member 8813030 22-Sep-16 0:12am    
@pwasser: I have not created the file.Its a log file that has been generated somewhere and I need to parse the file for further processing. So it will be very kind of you to help me solve the problem because to change the file format is not in my hands.
[no name] 22-Sep-16 6:10am    
Well what's the problem? You know the schema, just read the three lines and extract the data as required. Quite straightforward. I hope this is student work and nothing else because the file format will inevitably lead to other problems if it is not addressed.

1 solution

Parsing strings is a common path. Depending on what string class you have in your project you may use some split or strtok function.

A nice solution i found in this article with split and a article for strtok redeemed. After finding the substring you may need to remove white spaces and convert the string into some other data type.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900