Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi All,

I have a file like this
I am a student xyz in university"|USA|" in first year.
I am a student abc in university"|America|" in first year.
I am a student asd in university"|Italy|" in first year.


I have to parse the file and extract only the university name from the file and also the xyz,abc,asd
so i have written the following code
C++
//#include <stdio.h>

#include <fstream>
#include <string>
#include <iostream>
bool ParseLine(const std::string& line, std::string& key, std::string& value);
 
class Demo
{
public:
    typedef struct _Data 
    {
        std::string university;
		std::string name;
        
    } Data;
 
    Data data;
};
 
int main()
{
    std::ifstream ifs("Ca.txt");
    std::string line;
    std::string key;
    std::string value;
    Demo obj;
    while(!ifs.eof())
    {
        std::getline(ifs, line);
        if (ParseLine(line, key, value))
        {
            if (0 == key.compare("student"))
            {
                obj.data.name = value;
            }
         
			 else if (0 == key.compare("university"))
            {
				obj.data.content = value;
            }
            else
            {
                std::cerr<<"Unknow key:" << key << std::endl;
            }
        }
    }
    return 0;
}
 
bool ParseLine(const std::string& line, std::string& key, std::string& value)
{
    bool flag = false;
    
if (line.find("//") != 0)
    {
        size_t pos = line.find("|");
        if (pos != std::string::npos)
        {
            key = line.substr(0, pos);
			 size_t pos1 = line.find(";");
            value = line.substr(pos + 1, pos1);
            flag = true;
        }
    }
	
    return flag;
}


Two problems
When extracting the university value i get every thing like "USA|" in first year" but i want only USA and name logic dosen't work

Please guide
Posted

1 solution

1) Not sure why you check for ";" in ParseLine, but you would need to check for the 2nd "|", e.g.
C++
size_t pos = line.find("|");
if (pos != std::string::npos)
{
  size_t pos1 = line.find("|", pos + 1);
  value = line.substr(pos + 1, pos1 - pos - 1);
}


2) The easiest solution to the student name is to produce similar logic as the university value, e.g.
C++
size_t pos = line.find("student ");
if (pos != std::string::npos)
{
  size_t pos1 = line.find(" ", pos + 8);
  key = line.substr(pos + 8, pos1 - pos - 8);
}


Following this would then simply have the following in the main function:
C++
if (ParseLine(line, key, value))
{
  obj.data.name = key;
  obj.data.content = value;
}


(You should probably create a function inside the if statement to validate the key and/or value to ensure they are acceptable values)
 
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