Lets say I have an outer while loop to read each character and output it to console. I also want to flag a word if it is found and by using the peek method I can find the first instance of a word. Is there a way to peek multiple places ahead. For example, if I looking for the word "payday" . I know I can input this into a string and search a string, but I want to read files in binary mode and I don't want to take away any values from the outer loop. If I have an inner loop with a read method, those values are not then displayed via the outer loop.
Thanks
int main()
ifstream strm;
char *chr = new char;
strm.open("mytext.txt",ios::out | ios::binary);
while (strm.read(chr,1)
{
if (strm.peek() == 'p';
{
cout << "found a word beginning with 'p'" << endl;
//what if I want to read multiple characters ahead. Peek will read only one.
}
}
You can use seekg method to jump around within the istream.
If you open your file in binary mode, you can use tellg method to mark the position to jump to.
However, if you open your file in text mode, it is better to jump with offset instead (i.e. strm.seekg(offset, strm.cur)), due to how multibyte characters like newline are counted by tellg and seekg in text mode. So if you detect that the next character is going to be "p", then you can read the next n character, then jump back -n character if it's not what you're looking for.