Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have an query

Suppose i have the following line in a text file
Hi i am xyxabc and i work for xyz


I want the output to be "and i work for"

I have the following code
C++
#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 abc;
		
        
    } 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("abc"))
            {
				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;
}



I want the output to be and i work for that is before xyz.
Posted
Comments
CHill60 22-Feb-14 8:20am    
Please do not repost the same question File ponter maintaince error please guide[^]
Robert Clove 22-Feb-14 8:24am    
ok will take care of that any solution from ur side?
CHill60 22-Feb-14 8:36am    
Haven't got 'C++' at this location - yet ... slow internet connection means I won't be getting it any time soon either :-( I'll have a look later
Robert Clove 22-Feb-14 9:06am    
waiting for the solution
pharap@hotmail.com 23-Feb-14 9:03am    
I'd like to point out that this isn't how parsers work, it's a common misconception that a parser takes a stream of text and turns it into something else. In actual fact, a parser takes a stream of things called tokens, which are made by a lexical analyser(aka scanner/tokeniser/lexer). The parser then emits a parse tree, which is then used to create usable output. Example: http://2.bp.blogspot.com/-HyH1_FXl6eQ/TzC60mQP9nI/AAAAAAAAAFY/g2B31KnChus/s1600/aa1.JPG

1 solution

I won't go into much, but maybe this gets you started. Always try to start from the simplest version of the problem. I see something like this:

#include <string>
using namespace std;

int findSchoolEnd(const string & str) {
   size_t pos = str.find("xyxabc");
   if (pos == string::npos) return -1;
   return pos + string("xyxabc").length();
   }

int findStudentBegin(const string & str) {
   size_t pos = str.find("xyz");
   if (pos == string::npos) return -1;
   return pos;
   }

string getMiddleStuff(const string & str, size_t begin, size_t end) {
   if (begin == string::npos) return "";
   if (end == string::npos) return "";
   return str.substr(end, begin-end);
   }

int main() {
   string str = "Hi i am xyxabc and i work for xyz";
   size_t begin = findSchoolEnd(str);
   size_t end   = findStudentBegin(str);
   string result = getMiddleStuff(str, end, begin);
   }


Only begin working on the key comparisons after the core works, like the previous code does.

Good luck!
 
Share this answer
 
Comments
Robert Clove 25-Mar-14 12:28pm    
Can you please help me on this http://pastebin.com/hzzjN6KH

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