0

This question has been bothering me all day.
if I'm trying to pull out all strings that are in a txt file
and all start with "abcde"
is there anyway to find them, and copy the strings??
assuming the txt file contains lots of text

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by bigwhiteegg
0

I'm think of using getline()
by using pos=str.find(abcde)
I can locate the str and make a substr till the end of line or specified length
is that gana work? or any other faster way?

one more question
by using my method, can I stop the substr at certain char such as whitespace?

0

Sounds like your idea will work well. Try it and see.

I don't know about stopping at whitespace, but you can certainly find whitespace after your substring is found.

0

thanks to WaltP's help
I'm able to figure out a way to solve my problem
here is the structure of my code
hope it will help others

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
string imgURL, line;
size_t pos, found;
ifstream myfile;
myfile.open ("1.txt");
ofstream writefile;
writefile.open ("2.txt");
if(myfile.is_open())
{
  while(myfile.good())
  {
    getline(myfile, line);
    found=line.find("some text");
    if(found!=string::npos)
    {
      pos=line.find("some text");
      imgURL=line.substr(pos);
      //use method like erase or specify a range to copy the string you want
      writefile<<imgURL<<endl;
    }
  }
  myfile.close();
  writefile.close();
}

system("PAUSE");
return 0;      
}

Edited by bigwhiteegg: n/a

This question has already been answered. 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.