0

I am new with C++, I have a file that contains the following lines:

 ~~ 13!-!43??
CaKnX5H83G
 ~~ 107!-!22??
 ~~ 140!-!274??
 ~~ 233!-!75??
begin
 ~~ 143!-!208??
143
 ~~ 246!-!138??
 ~~ 79!-!141??
vC5FxcKEiN
 ~~ 60!-!201??
83
end
 ~~ 234!-!253??
 ~~ 51!-!236??

I want to read the content between the two words (begin, end) and remove all other separators ~~ !-! ??

I tried this code:

#include <iostream>
#include <fstream>
using namespace std;
int main() {
    ifstream my_file;
    my_file.open("file.txt", ios::in);

    if (!my_file) {
        cout << "No such file";
    }
    else {
        string ch;
        string qlline;
        while (1) {
            getline(my_file, qlline);
            if (my_file.eof()) {break;}
            if (qlline.find("end") == 0) {break;}
            if (qlline.find("begin") != 0) {continue;}
            my_file >> ch;
            cout << ch;

        }

The result is strange and not at all what I wanted to find! P.S: I don't know how not to take the separators (~~! -! ??) into consideration!

Any suggestion, modification, or a link please! Thanks in advance.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Phd student
  • 25
  • 1
  • 1
  • 8
  • What did you expect? – user4581301 Aug 23 '21 at 17:03
  • 2
    Handy reading: [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – user4581301 Aug 23 '21 at 17:04
  • i expected: 14320814324613879141 vC5FxcKEiN6020183 .. but the result is completely different. – Phd student Aug 23 '21 at 17:16
  • 3
    `while (1) { getline(my_file, qlline); if (my_file.eof()) {break;} ... }` should be `while (getline(my_file, qlline)) { ... }`. And `if (qlline.find("end") == 0)` should be either `if (qlline.find("end") == string::npos)` or simply `if (qlline != "end")`. And `if (qlline.find("begin") != 0)` should be either `if (qlline.find("begin") != string::npos)` or simply `if (qlline == "begin")`. – Remy Lebeau Aug 23 '21 at 17:22
  • thank u sir for answering me, but the output of this modification is empty – Phd student Aug 23 '21 at 17:30
  • That would be time to use your debugger to figure out what is going on. – drescherjm Aug 23 '21 at 18:48

1 Answers1

0
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream my_file("file.txt");

    if (!my_file) {
        std::cout << "No such file";
        exit(1);
    }

    std::string qlline;
    bool insideSection = false;
    while (getline(my_file, qlline))
    {
        // I have correctly read a line from the file.
        // Now check for boundary markers.

        if (qlline.find("end") == 0)   {insideSection = false;break;}
        if (qlline.find("begin") == 0) {insideSection = true; continue;}

        // Are we inside the section of the file I want.
        if (!insideSection) {
            continue;
        }

        // I am now inside the part of the file with
        // text I want.
        std::cout << qlline << "\n";
    }
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Thank u very much sir for your answer. However, the output is only the word "begin" !! and not the part between Begin and And! – Phd student Aug 23 '21 at 19:52
  • @Phdstudent - typo: `qlline.find("begin") != 0` should be `qlline.find("begin") == 0` – user4581301 Aug 23 '21 at 20:10
  • What about the delimiters ! in my case, I have three delimiters : '~~' '!-!' '??' Is There any link or proposition, please! – Phd student Aug 23 '21 at 20:40
  • @Phdstudent I recommend keeping questions narrow in scope so that they are useful to a wider group of future askers. You should ask a new question when the question cool-down times out. – user4581301 Aug 23 '21 at 20:55