Separating substrings and storing in C ++ variables

0

So, I created a function that reads a text file and saves, line by line, into a vector of strings by removing the commas.

The file entry is:

add $t2, $t3, $t4
sub $t5, $t6, $t7
addi $t6, $t7, 4

Applying the function:

void openFile(char** filePipe, std::vector<std::string> *lines){
std::ifstream filePine;
std::string line;

filePine.open(*filePipe);
if (!filePine.is_open()){
    std::cout << "Arquivo " << *filePipe << " não foi encontrado" << std::endl;
}else{
    std::cout << "Arquivo " << *filePipe << " foi encontrado" << std::endl;
    std::cout << "Prosseguindo operação: " << std::endl;
    while(!filePine.eof()){
        getline(filePine, line);
        //std::cout << line << std::endl;
        line.erase(std::remove(line.begin(), line.end(),','), line.end());
        lines->push_back(line);
    }
}
filePine.close();
}

Generate this output:

add $t2 $t3 $t4
sub $t5 $t6 $t7
addi $t6 $t7 4

Now I need to separate each substring of this into 4 different vectors, all of them according to string0 (add, sub, addi, etc) since not all commands have 4 substrings (in this case I would add a nullptr to those that have less than four).

But I'm not finding something that will split this. can you help me?

Example: practical: In the entry:

add $t2 $t3 $t4

I need the program to store:

std::vector<std::string> instruc = add;
std::vector<std::string> op1 = $t2;
std::vector<std::string> op2 = $t3;
std::vector<std::string> op3 = $t4;

And so on successively for each input line, giving push_back () in the respective vector.

    
asked by anonymous 14.10.2016 / 04:04

2 answers

0

One way is that strings are only separated by spaces. There must be others.

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
   // string de entrada, com varias substrings separadas por espacos
   string tokenString { "aaa bbb ccc" };

   // cria um stream de strings baseado na string de entrada
   istringstream ss { tokenString };

   // vetor onde serao guardadas as substrings extraidas do stream de strings
   vector<string> tokens;

   // variavel que recebera' cada substring extraida do stream de strings
   string token;

   // separando as substrings e guardando num vetor de strings
   while (ss >> token)
      tokens.push_back(token);

   // mostrando as substrings que foram separadas
   for (const string& token: tokens)
       cout << "* " << token << endl;
}

PS. I love C ++:)

    
14.10.2016 / 04:15
0

C ++ does not seem to have a standard split function like Java or Javascript, however you can build one as follows:

#include <sstream>
#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<string> strings;
    istringstream f("denmark;sweden;india;us");
    string s;    
    while (getline(f, s, ';')) {
        cout << s << endl;
        strings.push_back(s);
    }
}

This code has been removed from this question here: link

I believe this will solve the problem.

    
14.10.2016 / 04:15