Massimo D. N.
Massimo D. N.

Reputation: 316

Split a line (string) into more strings

Edit code and question

i want to split a string (line read from .txt file) into more strings:

1 REP615624/1 BYZ12345 90 12.516 20.709

i want to put 1 in position REPxxx in partNumber BYZxxx in byz 90 in rotation 12xxx in x 20xxx in y and so on (20.709 terminates wiht \n and the there are other lines)

in c++ i used

std::ifstream leggiROF(nomeFileTxt.c_str());    // apre in lettura il file ROF.txt
while(!leggiROF.eof())                          // finché non raggiunge la fine del file
{
    getline(leggiROF, posizione, ' ');          // legge la riga fino allo spazio e mette contenuto in posizione
    getline(leggiROF, partNumber, ' ');         // legge la riga fino allo spazio e mette contenuto in part number
    getline(leggiROF, byz, ' ');                // legge la riga fino allo spazio e mette contenuto in byz
    getline(leggiROF, rotazione, ' ');          // legge la riga fino allo spazio e mette contenuto in rotazione
    getline(leggiROF, x, ' ');                  // legge la riga fino allo spazio e mette contenuto in coordinata x
    getline(leggiROF, y, '\n');                 // legge la riga fino allo '\n' e mette contenuto in coordinata y
} // end while

leggiROF.close();

I want to convert it in QT. I'm trying

void fileTxt::setContaRigheFileTxt(Ui::Dialog *ui)
{
    fileName = QFileDialog::getOpenFileName(0, QObject::tr("Apri File"),
                                                "C:\\Users\\Massimo Di Natale\\Documents\\Programmi C++ 11\\Programmi_QT\\Ericsson",
                                                QObject::tr("File ROF (R*.txt)")
if (!fileName.isEmpty())                        // se non è vuoto
    {
        QFile file(fileName);
        if (!file.open(QIODevice::ReadOnly))
        {
                QMessageBox::critical(0, QObject::tr("Errore"), QObject::tr("Non posso aprire questo file"));
                return;
        } // end if
        QTextStream in(&file);                      // legge il file .txt
        while(!in.atEnd())                          // finchè non raggiunge la fine del file
        {
                ui->textEdit->append(in.readLine());

                /* WANT TO PUT each part before ' ' in a different string */
                // pos=.....
                // pNumb=...
                // ecc...
                /*                                                        */

        } // end while
        file.close();                               // chiude il file aperto per la lettura
    } // end if
}

but don't how to do it

Upvotes: 0

Views: 218

Answers (4)

Massimo D. N.
Massimo D. N.

Reputation: 316

solved modifing While loop

while(!in.atEnd())                          // finchè non raggiunge la fine del file
    {
        //ui->textEdit->append(in.readLine() + "elab TXT");
        QString line=in.readLine();
        QStringList list=line.split(" ");

        posizione=list[0];          // legge la riga fino allo spazio e mette contenuto in posizione
        partNumber=list[1];         // legge la riga fino allo spazio e mette contenuto in part number
        byz=list[2];                // legge la riga fino allo spazio e mette contenuto in byz
        rotazione=list[3];          // legge la riga fino allo spazio e mette contenuto in rotazione
        x=list[4];                  // legge la riga fino allo spazio e mette contenuto in coordinata x
        y=list[5];                 // legge la riga fino allo '\n' e mette contenuto in coordinata y
    } // end while

Upvotes: 0

Andreas DM
Andreas DM

Reputation: 10998

You can use right shift operator (>>) in a loop:

fstream file("file.txt");
string position, partNumber, byz, rotation, x, y;
while (file >> position >> partNumber >> byz >> rotation >> x >> y) {
    // do work
}

Upvotes: 0

Michel Billaud
Michel Billaud

Reputation: 1826

Splitting using a istringstream (C++11 and later)

vector<string> split(const string & s)
{
    vector<string> tokens, t;
    istringstream in {s};
    while (in >> t) {
       tokens.push_back(t);
    }
    return tokens;
}

Or use something from Boost http://www.cplusplus.com/faq/sequences/strings/split/

Upvotes: 0

Don't know if this answers to your question but it's a split function I used for splitting strings in C++:

//Split string -- reusable
vector<string> splitString(string str, string delimiter)
{
  vector<string>v;
  string token;
  size_t pos = 0;

  while((pos = str.find(delimiter)) != string::npos)
    {
      token = str.substr(0, pos);
      v.push_back(token);
      str.erase(0, str.find(delimiter) + delimiter.length());
    }
  v.push_back(str);
  return v;
}

Upvotes: 3

Related Questions