Compare data from two different files and write C ++ result

0

I'm having trouble executing the following algorithm:

Based on the information in the '/ etc / passwd' file, pick up each user's group code and search for it in the '/ etc / group' file. The result should be written to the file '/tmp/resultado.txt' in the format 'user -> code - > group '.

My idea would be to store the data in struct (user, group id and group name), then put play into a MAP.

At the moment I am able to write the 3 information (idgroup, userName, groupName) I need inside the struct, however when I display there are several empty lines at the end of the result that should not be there.

Here is some code that has already been implemented:

using namespace std;

struct Pessoa {
    string nome;
    string codgrupo;
    string nomeGrupo;

};



int main() {

    typedef std::map<string, string> Grupo; // chave , <Pessoa>  - associ<Pessoa>string nomeUser;
        map<string, string>::iterator itmap;

    string temp;
    Pessoa user[200];
    int posicaoUser =0;
    string nomeUser;

    Grupo grupo;




    std::ifstream arq("/home/ton/teste/passwd2");
    if (arq.is_open()) {
        while (getline(arq, nomeUser) ) {
            temp = "";
            for (int i = 0; i < nomeUser.size(); i++) {
                if (nomeUser[i] != ':') {
                    temp = temp + nomeUser[i];
                } else {
                    break;
                }
            }
            user[posicaoUser].nome = temp;
            posicaoUser++;
        }   
    } else {
        cout << "Não encontrado!";
    }
    arq.close();



    const int MAX_BUFFER = 2048;
    char buffer[MAX_BUFFER];
    string temp2;
    int count = 0;
    int tempFind;
    string tempString;

    string command="cut -d: -f 1 /home/ton/teste/group2";
    FILE *stream = popen(command.c_str(), "r");

    string command2="cut -d: -f 3 /home/ton/teste/group2";
    FILE *stream2 = popen(command2.c_str(), "r");

    while (!feof(stream) && !feof(stream2)){

        if (fgets(buffer, MAX_BUFFER, stream) != NULL){
        string arq = buffer;
        user[posicaoUser].nomeGrupo = arq;
        posicaoUser++;
        }


        if (fgets(buffer, MAX_BUFFER, stream2) != NULL){
        string arq = buffer;
        user[posicaoUser].codgrupo = arq;
        posicaoUser++;
        }

    }

    pclose(stream);
    pclose(stream2);




    // Retorna dados da struct
    for (int i = 0; i < sizeof(struct grupo); i++) {
        cout << user[i].nome << endl;
        //cout << user[i].codgrupo << endl; // tras caracteres em branco antes de mostrar o nome
        //cout << user[i].nomeGrupo <<endl;  /// tras caracteres em branco antes de mostrar o nome

    std::ofstream Hypnos_FILE;

    Hypnos_FILE.open("/home/ton/Arquivo.txt", std::ios::app);

    Hypnos_FILE << user[i].codgrupo, user[i].nomeGrupo;

    Hypnos_FILE.close();

    }




    // Adiciona dados no MAP
    for (int i=0; i < posicaoUser; i++){
        grupo.insert(pair<string, string> (user[i].codgrupo, user[i].nomeGrupo));

        //cout << user[i].codgrupo, user[i].nomeGrupo;
    }



    return 0;
}

Any suggestion is welcome, grateful.

    
asked by anonymous 02.11.2018 / 19:45

0 answers