I read the text from the file wivod.txt. The file is:

13.34 *Длительность приема сигнала (м.с) 48 *Коэффицент усиления (dB) 1 *Заполнение импульса 48000 *Частотный диапазон 0.1 *Множитель амплитуды 

The sign "*" that after it is an explanatory text

I want to count to "*", then go to the next drain and take a variable (number) into the array.

Question: How to count to the text label "*"

  • And just read the number, and then remove everything to the end of the line does not fit? - Harry

4 answers 4

 QString str = "13.34 *Длительность приема сигнала (м.с) "; str = str.mid(0, str.indexOf("*")); 

    Use QString::split :

     QStringList list = str.split("*"); if (list.isEmpty()) continue; double variable = list[0].toDouble(); 
    • QStringList list = str.split ("*"); visited swears at him - timob256
    • @ timob256 What does it mean swearing? Give an error code. The code that I wrote, tested and working - Bearded Beaver
    • Not sure, but there may be a problem in the space after the number, as a result of which a conversion error is possible. - shaman888

    You can do this:

     #include <iostream> #include <string> #include <limits> template<class T> struct Line{ T value; std::string name; }; template<class T> std::istream& operator>>(std::istream &is, Line<T> &line){ is >> line.value; is.ignore(std::numeric_limits<std::streamsize>::max(), '*'); is >> line.name; return is; } int main(){ Line<double> line; std::cin >> line; std::cout << line.name << " " << line.value; } 

    In the main function, I read the value from std::cin , but you can use any input stream std::sstream , std::fstream or even some custom implementation.

    UPD . If suddenly you need only the first number, you can do this:

     #include <iostream> #include <limits> template<class T> T get(std::istream &is){ T result; is >> result; is.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); return result; } int main(){ double d; d = get<double>(std::cin); } 

    The get function reads the first value, and skips the string to the end. Again, instead of std::cin you can use std::sstream or std::fstream

       QFileDialog dlg(this); QString D_path = "C:/WORK/"; QString D_format = "txt"; isRead = dlg.getOpenFileName(this,trUtf8("Открываем условия запуска "),D_path,trUtf8("DUMP(*.%1)").arg(D_format)); qDebug()<< isRead; qDebug()<< "мы тут загружаем"; // переводим в из QString в char char* value_name = new char[255]; char* sometext=isRead.toAscii().data(); value_name = sometext; char peremen_t[255]; // результат будем складывать сюда int t = 0; // счетчик peremen_t QFile file(value_name); if(file.open(QIODevice::ReadOnly |QIODevice::Text)) { while(!file.atEnd()) { //читаем строку QString str = file.readLine(); for (int i =0; i<str.size();++i) { if(str[i] == '*') { break; // если str[i] == '*' то сбрасывает строку } peremen_t[t] = str[i].toAscii(); t= t+1; } } } else { qDebug()<< "don't open file"; } qDebug()<<"peremen_t " << peremen_t; 

      And here is the result: peremen_t 13.34 48 1 48000 0.1