Thread: Comparing lines based on a certain part

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    4

    Question Comparing lines based on a certain part

    I have this program I am writing that cleans up a xml file and adds new lines to a settings section from a txt file. Part of it I have a section labeled // <settings> Part in my code. It is during or after that section, either is fine, I would like to compare the lines to make sure they are not duplicated but ignore their setting in this case True and False and consider them identical if one is set to true and the other set to false and only keep the second line and discard the first. Here is an example of how the settings look:

    <setting1>true</setting1>
    <setting2>false</setting2>
    <setting3>true</setting3>
    <setting1>false</setting1>
    <setting4>true</setting4>
    <setting2>true</setting2>

    So in the end I would like the first setting 1 to be removed and the second setting 1 to stay and same thing for setting 2. Keep in mind this is an example as the settings have different names and sometimes contain the same words.

    I appreciate any help.

    Thanks,
    V

    Code:
    //
    
    
    #include <stdio.h>
    #include <fstream>
    #include <sstream>
    #include <iostream>
    #include <string>
    #include <cctype>
    #include <cstdlib>
    #include <set>
    #include <vector>
    #include <algorithm>
    #include <cassert>
    #include <Windows.h>
    using namespace std;
    
    
    bool isSpace(unsigned char c) {
        return ( c == '\r' ||
            c == '\t' || c == '\v' || c == '\f');
    }
    
    
    int main()
    {
    
    
        const string Dir{ "C:/synergyii/config/" };
        ifstream in_config{ Dir + "clientconfig.xml" },
            in_newlines{ Dir + "newlines.txt" };
        ofstream out{ Dir +  "cltesting.txt" };
    
    
        vector<string> vlines31;
        vector<string> vlines32;
        set<string>    slines31;
        set<string>    slines32;
    
    
        for (string line31; getline(in_config, line31); vlines31.push_back(line31))
            if (line31.find("<settings>") != string::npos) {
                vlines31.push_back(line31);
                break;
            }
    
    
        for (const auto& v : vlines31)
            out << v << '\n';
    
    
        // <settings> Part
        
        for (string line32; getline(in_config, line32) && line32.find("</settings>") == string::npos; ) {
            line32.erase(remove_if(line32.begin(), line32.end(), isSpace), line32.end());
            line32.erase(line32.find_last_not_of(" ") + 1);
            const auto& result = slines32.insert(line32);
            if (result.second)
                vlines32.push_back(line32);
        }
    
    
        for (string line32; getline(in_newlines, line32);) {
            line32.erase(remove_if(line32.begin(), line32.end(), isSpace), line32.end());
            const auto& result = slines32.insert(line32);
            if (result.second)
                vlines32.push_back(line32);
        }
    
    
        vlines32.erase(unique(vlines32.begin(), vlines32.end()), vlines32.end() );
    
    
        for (auto it = vlines32.cbegin(); it != vlines32.cend(); ++it)
            out << '\t' << '\t' << *it << '\n';
    
    
        out << '\t' << "</settings>\n";
        out << "</config>\n";
    
    
        in_config.close();
        out.close();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I would suggest you use an XML parsing library instead of attempting your own ad-hoc approach.

    reading an XML file in a C++ program - Stack Overflow

    All it takes is someone to decide to edit their config file to say try and make a comment of some settings with
    Code:
    <!-- <settings>
    and all of a sudden, your code is in a world of pain.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im printing 3 lines of the same on all my lines, please help
    By mountaindrew11 in forum C Programming
    Replies: 8
    Last Post: 04-11-2016, 10:57 AM
  2. Replies: 15
    Last Post: 02-25-2013, 08:49 PM
  3. TurboC based, Graphics based program with weird error!
    By sidx64 in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2012, 12:48 PM
  4. Translate a string matrix[lines][4] into one[lines][2]
    By muacsom in forum C Programming
    Replies: 3
    Last Post: 06-16-2012, 04:45 PM
  5. Replies: 11
    Last Post: 10-07-2008, 06:19 PM

Tags for this Thread