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

Dani AI

Generated

A compact, reliable pattern for this problem: read the file line-by-line, search for the prefix, then take the characters from that position up to a delimiter (whitespace, punctuation, etc.). already sketched the find+extract idea and pointed to string facilities — a few practical refinements improve robustness and performance.

Use while (std::getline(...)) rather than while (file.good()), advance the search position after each match to find multiple occurrences per line, and avoid unnecessary allocations by using std::string_view when only a read-only view is required. The example below extracts tokens that start with needle and stop at the first whitespace character:

#include <fstream>
#include <string>
#include <string_view>

int main() {
  std::ifstream in("1.txt");
  std::ofstream out("2.txt");
  std::string line;
  const std::string needle = "abcde";
  const std::string delims = " \t\r\n";
  while (std::getline(in, line)) {
    size_t pos = line.find(needle);
    while (pos != std::string::npos) {
      size_t end = line.find_first_of(delims, pos);
      std::string_view sv(line);
      sv.remove_prefix(pos);
      if (end != std::string::npos) sv = sv.substr(0, end - pos);
      out << sv << '\n';
      pos = line.find(needle, pos + needle.size());
    }
  }
}

Notes and tips:

  • find_first_of is useful to stop at any of several delimiters (reference).
  • std::string_view avoids copying the substring; do not let the view outlive the original std::string (reference).
  • For more complex patterns (word boundaries, punctuation rules), std::regex or std::sregex_iterator works but can be slower; use it only if pattern logic becomes nontrivial (reference).
  • Beware of non-ASCII whitespace and line splitting: if matches can span lines, read larger chunks or the whole file and scan accordingly.
  • Drop nonportable bits like system("PAUSE") and always check file open state.

Recommended Answers

All 4 Replies

Look up the methods of the string class. There's a lot you can do with it.

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?

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.

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;      
}
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.