There is my code; By the method of elimination, I found out that the "floating" problem in the section below (in C ++, I had never written anything longer than five lines ..):
... using namespace std; ... void myload (const char *fname) { bool fnd, work = false; char buffer[BUF_SIZE]; string str = ""; ifstream input(fname); while (input.getline(buffer, BUF_SIZE)) { if (strstr(buffer, "sign_begin") && !work) { work = true; fnd = true; } if (fnd) { str = str + buffer + "\n"; } if (strstr(buffer, "sign_end") && work) { break; } } ... It is assumed that in the current directory is the file fname , which contains the sign_begin and sign_end .
Logic suggests that you may need to somehow close the file? For example, I insert
input.close()at the end of thewhileblock and everything works correctly for me (in a loop, a lot of loops ..). however, I insert the same command immediately after thewhileblock, and the code does not compile .. this is hardly the right solution ..While asking the question, I very strongly began to suspect that the problem was in
bool fnd, work = false;- if you individually initialize each variable, then everything seems to be working fine too. I do not know where I have such a construction in my head .. And how will the compiler perceive it? I am using g ++For that matter, perhaps there is a more "elegant" solution to the problem, which is solved by the given code segment (?)