编辑代码和问题
我想将一个字符串(从 .txt 文件读取的行)拆分成更多的字符串:
1 REP615624/1 BYZ12345 90 12.516 20.709
我想放 1位就位 partNumber 中的 REPxxx BYZxxx 在 byz 90度旋转 12xxx 英寸 y 中的 20xxx 依此类推(20.709 以\n 终止,还有其他行)
在c++中我用过
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();
我想在QT中转换它。我在努力
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
}
但是不知道怎么做
最佳答案
不知道这是否回答了您的问题,但这是我在 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;
}
关于c++ - 将一行(字符串)拆分成更多的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39996694/