My objective is to read in a line from a text file, then search that line for a particular set of characters/numbers, and finally, if the line contains the particulars, write the line to another file.

I am fine with all of the coding, except the part of searching the line for the characters.

The source file is formatted in the following way:

text;numbers

I will be searching for particular numbers and/or text.

I hope I am giving enough information.

Thanks ahead of time.

If you use std::string for the input line and the search line, then when a line is read just use std::string's find() method to see if it contains the search line text.

std::string line;
std::string search_text = "World";

// read file not shown.  When line is read, do this:
if(line.find(search_text) != string::npos)
{
    // found it
    // now do somethig with this line
}

Ok, thank you very much for your timely response. I have that part all working, and it finds what I am looking for. How hard would it be to remove some of the text from the line being read in?

The file is formatted the following way:
text;number

Now, after it is confirmed that the line contains the search string, I would like to remove said search string from the line before writing it a destination source.

use std::string's substr() method. find() returns the position of the first character in the search string.

  1. Get substring from position 0 to start of search string
  2. Get substring from end of search string to end of the original string

For example, if the line is "Now is the time", and search string is "the" then you need to get "Now is " and " time"

I am having an issue with the line:

newString.erase (pos, myString.end());

string removeSemi(string myString) {
    size_t pos;
    string newString;
    string subString =";";

    pos = myString.find(subString);

    newString = myString.erase (pos, email.end());

    return newString;
}

I am trying to find the semi-colon in the string and then erase from the semi-colon to the end of the string and keep the new string.

line 8 is wrong. The second parameter must be an iterator of myString. If you want to erase from pos to end of string then you don't need the second parameter at all. See this link Or for what you are doing you could also use substr() method
newString = myString.substr(0,pos-1)

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.