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

<<<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

2
Contributors
1
2
Views
9 Years
Discussion Span
Last Post by pcgeek123

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.