Thread: Parsing TXT messages

  1. #1
    Registered User
    Join Date
    Dec 2021
    Posts
    5

    Parsing TXT messages

    Hello world of C++ programming .

    I am trying to parse a series message from text file that has different unique patterns and save them as txt files using C++ programming.


    I have as input txt file:

    Code:
        [#11:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        INFO isn't NULL
        [#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
        [#13:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        PERFECT isn't NULL
        [#4:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        Time is here [Tick:135055] , Time:  17, index: 608, CastedType:20002, area :0
        [#15:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        [#16:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
        [#17:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        [#8:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        time is here [Tick:135055] , Time:  17, index: 608, CastedType:20002, area :0
        [#16:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        [#14:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
        [#18:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        [#6:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        Time is here [Tick:135055] , Time:  17, index: 608, CastedType:20002, area :0

    this is the type formats of all rows that txt have , so each row is repeated on given txt file and it has its own unique pattern as I showed above, where the key words [INFO] , [PERFECT] are not changed per the message those key words values are not changed in this message pattern.
    consider each row is a new message , so at each row there is a new message starts irrelative to the other messages.


    what Im trying to implement in C++ a function that reads line by line the given txt file to a function as input and all rows there has different patterns of messages as I mentioned above and to dump all rows / messages that has the certain type of keyword [PERFECT] :

    Code:
        [#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]

    to another txt file. so then if I go to another txt file I shall see all rows there has this type of messages:

    Code:
        [#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]

    Now after sniffing this type of message that has keyword [PERFECT] from the given txt(input txt) , I need to read line by line the new txt file that I generated that has the certain message type and then take the load index values and dump them in another txt file that has just the values of load index.


    Function description:

    Given txt file to the function as input that txt file has all messages patterns that I mentioned above and at each row there is new message:

    Code:
        [#11:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        INFO isn't NULL
        [#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
        [#13:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        PERFECT isn't NULL
        [#4:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        Time is here [Tick:135055] , Time:  17, index: 608, CastedType:20002, area :0
        [#15:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        [#16:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
        [#17:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        [#8:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        time is here [Tick:135055] , Time:  17, index: 608, CastedType:20002, area :0
        [#16:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        [#14:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
        [#18:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        [#6:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
        Time is here [Tick:135055] , Time:  17, index: 608, CastedType:20002, area :0


    Results/output of the function:


    1. Generating txt file that has all rows of the **certain pattern** that I explained above (all rows that has word **[PERFECT]** so the generated txt file shall be having all messages / rows that has **[PERFECT]** :

    Code:
        [#12:25]**[PERFECT]**[0x0015a] process returned as NULL load index[1] , length[20] , type[0]
        [#16:25]**[PERFECT]**[0x0015a] process returned as NULL load index[1] , length[20] , type[0]
        [#14:25]**[PERFECT]**[0x0015a] process returned as NULL load index[1] , length[20] , type[0]

    2. Then generating a another new txt file for the load index values which in my case load index values found inside [ ] of the word load index ( load index [value] ), so the function shall dump in new txt file the values of the load index **as column** into the another new generated txt file :


    1
    1
    1




    How to parse and implement in C++ that function functionality?




    thanks alot for any cooperation !
    Last edited by DodoMartin; 12-14-2021 at 11:16 AM.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    The first part is easy.
    You need to open the input with a std::fstream and open the output file with a std:fstream. Then you read the lines with std::getline. To check if a line contains [PERFECT] you can use the find method of the string. If the lines contain the pattern write it to the putput file.
    Hope this makes sense.

  3. #3
    Registered User
    Join Date
    Dec 2021
    Posts
    5
    Quote Originally Posted by thmm View Post
    The first part is easy.
    You need to open the input with a std::fstream and open the output file with a std:fstream. Then you read the lines with std::getline. To check if a line contains [PERFECT] you can use the find method of the string. If the lines contain the pattern write it to the putput file.
    Hope this makes sense.
    Thanks ,
    frankly not fully got you, can you please elaborate more? even by some pseudo code would be fine.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
     
    int main() {
        ifstream in("input.txt");
        ofstream out1("out1.txt"), out2("out2.txt");
        for (string line; getline(in, line); ) {
            if (line.find("[PERFECT]") != line.npos) {
                out1 << line << '\n';
                size_t pos = line.find("load index[") + 11;
                out2 << line.substr(pos, line.find(']', pos) - pos) << '\n';
            }
        }
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Dec 2021
    Posts
    5
    Quote Originally Posted by john.c View Post
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
     
    int main() {
        ifstream in("input.txt");
        ofstream out1("out1.txt"), out2("out2.txt");
        for (string line; getline(in, line); ) {
            if (line.find("[PERFECT]") != line.npos) {
                out1 << line << '\n';
                size_t pos = line.find("load index[") + 11;
                out2 << line.substr(pos, line.find(']', pos) - pos) << '\n';
            }
        }
    }
    Thanks ,
    I understand your approach so if I need for example to dump the length value (length[value]) instead of load index then the function is like this:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main() {
        ifstream in("input.txt");
        ofstream out1("out1.txt"), out2("out2.txt");
        for (string line; getline(in, line); ) {
            if (line.find("[PERFECT]") != line.npos) {
                out1 << line << '\n';
                size_t pos = line.find("length[") + 11;
                out2 << line.substr(pos, line.find(']', pos) - pos) << '\n';
            }
        }
    }
    



    So the generated txt out2 file will be length values of sniffed messages of pattern [PERFECT] as following:
    20
    20
    20


    Right? but once Im trying to run that in my compiler Im getting wrong chosen value and not the length values of the sniffed messages.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    11 is not a magical number. It is the length of the string "load index[". So if you are finding "length[" instead, you need to add 7. You could make a function to return the value in brackets after a string:
    Code:
    string find_value(const string& line, const string& tag) {
        size_t pos = line.find(tag) + tag.size();
        return line.substr(pos, line.find(']', pos) - pos);
    }
    
    // in main 
    out << find_value(line, "length[") << '\n';
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    Dec 2021
    Posts
    5
    Quote Originally Posted by john.c View Post
    11 is not a magical number. It is the length of the string "load index[". So if you are finding "length[" instead, you need to add 7. You could make a function to return the value in brackets after a string:
    Code:
    string find_value(const string& line, const string& tag) {
        size_t pos = line.find(tag) + tag.size();
        return line.substr(pos, line.find(']', pos) - pos);
    }
    
    // in main 
    out << find_value(line, "length[") << '\n';
    Sorry for that, now I'm fully understand you just missed that offset understandings!

    So mate john if I would take my input file to the function from directory: /tmp/prints
    And I want to generate the output txt files of that function also into that directory.

    So I shall give in
    Ifstream in("/tmp/prints/input.txt")
    ofstream out1("/tmp/prints/output1.txt"), out2("/tmp/prints/output2.txt").

    Right? Much thanks for your clarifications.

  8. #8
    Registered User
    Join Date
    Dec 2021
    Posts
    5
    Quote Originally Posted by DodoMartin View Post
    Sorry for that, now I'm fully understand you just missed that offset understandings!

    So mate john if I would take my input file to the function from directory: /tmp/prints
    And I want to generate the output txt files of that function also into that directory.

    So I shall give in
    Ifstream in("/tmp/prints/input.txt")
    ofstream out1("/tmp/prints/output1.txt"), out2("/tmp/prints/output2.txt").

    Right? Much thanks for your clarifications.


    Am I right? thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM
  2. messages to more than 1 win..
    By ballackz in forum Windows Programming
    Replies: 1
    Last Post: 12-26-2004, 08:20 AM
  3. PM Away Messages
    By drdroid in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 08-02-2003, 10:02 PM
  4. 2 messages
    By lectrolux in forum C Programming
    Replies: 4
    Last Post: 05-06-2003, 01:39 PM
  5. Messages not getting through
    By PsychoBrat in forum Windows Programming
    Replies: 4
    Last Post: 03-15-2002, 12:27 AM

Tags for this Thread