I have the text file (in1.text)

<<<start>20>>everything going fine or not<start>i do not know but I am trying<end><start>Trying is but we can do<startabcdefghijklmnopqrstuvwxyz<end>
<<<start>20>>Trying to do things<end><<<start>20>>well try try and try again<end>


I have to extract tag <<<start>20>> from file.My code is

#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
#include<string>
#include<algorithm>

using namespace std;

int main()
{  
       
    std::ifstream fin;
    fin.open("in1.txt");
    static int pos;
    int array[10];
        if (fin) {
         std::stringstream ss;
         ss << fin.rdbuf();
         std::string contents = ss.str(); 
      

 for(int i =0,search;(search= contents.find("<<<start>", i))!= string::npos;i= search + 1)                       
                           
    {

    start[search] = "<<<start>";
    array[pos]=search;
    pos++;
        
    }//end for

    for(int itr=0;itr<=pos;itr++)
     {
     int number=array[itr];
     string substring=contents.substr(number,number+13);
     cout<<substring<<"\n";
     number=0;

     }//end for

               }//end fin

}//end main

The code is giving output

[replyfast@localhost NEW]$ ./a.out
<<<start>20>>
<<<start>20>>i am all right ,just trying to do things<end><<<start>20>>well try try and try again<end>

<<<start>20>>well try try and try again<end>

Aborted

The first output is ok,but subsequent output gives some additional lines.How to rectify code so that it gives only <<<start>20>>
I am working on redhat linux(g++ compiler)


Regards
replyfast

from looking at your code I can see a few problems.

1st you need to read in your text file before you can do anything and the structure you are using doesnt work. fin.open("in1.txt"); you didnt specify a file path like this.
fin.open("C:\\in1.txt");. where ever you txt.file is loacated, go there and hit properties and copy and paste the file path but be sure to add an extra \ to where they are in the path or it will not open.

2nd I didnt notice if you had a check to see if your file open properly.

3rd you can use the getline.(fin) function to pull out 1 string of text at a time.

Not sure if this helped but if it does and you need more help let me know.

Just noticed you are working with Linux, sorry I cant help you there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.