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 .

  1. Logic suggests that you may need to somehow close the file? For example, I insert input.close() at the end of the while block and everything works correctly for me (in a loop, a lot of loops ..). however, I insert the same command immediately after the while block, and the code does not compile .. this is hardly the right solution ..

  2. 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 ++

  3. For that matter, perhaps there is a more "elegant" solution to the problem, which is solved by the given code segment (?)

  • all code in studio can be - V. Rotenberh
  • All code takes 2000+ lines. The problem was localized for a long time, so I only brought the problem code. Comrade Harry has already given a comprehensive answer, for which he thanks. And thank you for your concern :) - cpp beginner

1 answer 1

  1. The file closes itself when it goes out of scope when the input destructor is called.
  2. fnd variable is uninitialized , i.e. can be anything. Initialize it, no problem: bool fnd = false, work = false;
  3. Do you want to get the lines between the lines that have the substrings sign_begin and sign_end inclusive? excluding these lines? Describe more precisely what you want - then it will be clearer that you answer the third point ...

Update

I would do something like this:

 bool found = false; while (input.getline(buffer, BUF_SIZE)) { if (!found && strstr(buffer, "sign_begin")) { found = true; continue; } if (found) { if (strstr(buffer, "sign_end")) break; str = str + buffer + "\n"; } } 

Those. sign_begin is not found sign_begin , we check the lines for its presence. Found - put a flag, went to the next line. If found (flag set) - check for sign_end , find - exit from loop (are you looking for one entry?). Not found - the string is added to str . It is clear that I did not compile, I just show the scheme.

If you need to look further - the second entry - then

  if (strstr(buffer, "sign_end")) break; 

change to

  if (strstr(buffer, "sign_end")) { found = false; continue; } 
  • 3. get the line between the lines in which there are substrings sign_begin and sign_end, excluding these lines. - cpp beginner
  • Then I would have cost one flag, not two. Now add the code to the answer. - Harry
  • Thank you, everything worked perfectly! - cpp beginner