Moving the line
stringstream aa;
just before the line
aa << b;
solves the problem for me.
This is perhaps caused by use of aa
both as an input stream as well as output stream. Not sure of the details.
Update
Here’s your program with a bit of error checking code thrown in.
#include <iostream>
#include <sstream>
#include <string>
#include <string.h>
using namespace std;
char a;
char b[256];
string str2 = "A";
string fileline1 = "ABC";
int i;
int x;
stringstream aa;
int main()
{
while ( i < 7 )
{
std::size_t found = fileline1.find(str2);
if (found!=std::string::npos)
{
cout << "first '" << str2 << "' found at: " << found << '\n';
strcpy(b, str2.c_str());
for ( int x=0; b[x] != '\0'; ++x )
{
b[x]++;
}
}
aa << b;
if ( !aa.good() )
{
cout << "The stringstream is not good.\n";
}
aa >> str2;
if ( aa.eof() )
{
cout << "The stringstream is at eof.\n";
aa.clear();
}
if ( !aa.good() )
{
cout << "The stringstream is not good.\n";
}
i++;
}
}
Output
first 'A' found at: 0 The stringstream is at eof. first 'B' found at: 1 The stringstream is at eof. first 'C' found at: 2 The stringstream is at eof. The stringstream is at eof. The stringstream is at eof. The stringstream is at eof. The stringstream is at eof.
Without the block
if ( aa.eof() )
{
cout << "The stringstream is at eof.\n";
aa.clear();
}
The state aa
of was such that it wasn’t doing anything with the lines:
aa << b;
aa >> str2;
Hence, str2
was not being changed at all.
1
solved C++ while loop resetting variables?