Separating substrings and storing in C++ variables

Asked

Viewed 714 times

0

So I created a function that reads a text file and stores, line by line, in a string array by removing 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();
}

Generates me 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.) because not all commands have 4 substrings (in case I would add a nullptr in those that have less than four).

Only I’m not finding something that serves as a split for it. Could you help me?

Example: practical: practical: At the entrance:

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 successively for each input line, giving push_back() on the respective vector.

2 answers

0


One way is this, considering that strings are separated only by spaces. There must be other.

#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++ :)

0

C++ apparently doesn’t have a standard split function like in 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 was taken from that question here: https://stackoverflow.com/questions/5167625/splitting-a-c-stdstring-using-tokens-e-g

I believe that solves the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.