If I am using the ifstream.read method in c++ in an outer loop when reading the contents of a file and I then have an inner loop to ascertain a combination of values (in the example below, 5 bytes), then something strange happens. (strange to me anyway).
Lets say I am reading each character using the outer loop and I want to know when I reach the word
wednesday. I first look for a 'w' and then use the inner loop (also a read method) to ascertain whether the next 5 characters are 'e','d','n','e','s' to check if I have found the word "wednesday".
If I do this the output up until I find the outer
read while loop outputs a
'w' to the console.
The inner loop then outputs
ednes
Then the outer loop goes on to output
day.
I don't understand why the data read in the outer while loop skips over the values I am reading from an inner while loop. Is there a way of undoing what the inner loop does in order to output everything in the file through the outer loop? I hope this makes sense. Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
int main()
{
ifstream mystream;
char *mychar = new char;
char *target = new char;
mystream.open("test.txt",ios::out | ios::binary);
while(mystream.read(mychar,1))
{
cout << "///////////////////////" << mychar << endl;
if (*mychar == 'w')
{
mystream.read(target,5);
cout << target << endl;
}
}
|