[Solved] A brute qtextstram-autodeleter



  • let us see this snippet:
    @
    void Seconda::writeNewSection(QString linguaOrigine, QString linguaFinale)
    {
    linguaOrigine[0] = linguaOrigine[0].toUpper();
    linguaFinale[0] = linguaFinale[0].toUpper();
    if(linguaFinale.compare("Tedesco") == 0 || linguaFinale.compare("Deutsch") == 0)
    {
    QString tmp = linguaFinale;
    linguaFinale = linguaOrigine;
    linguaOrigine = tmp;
    }
    QString lingue = linguaOrigine + " " + linguaFinale;
    QFile f(QDir::currentPath() + "/listaSezioni.txt");
    if(f.open(QIODevice::Append | QIODevice::Text | QIODevice::ReadWrite))
    {
    QTextStream stream (&f);
    if(stream.readAll().contains(lingue))
    {
    f.close();
    emit signal_return();
    }
    else
    {
    qDebug()<<"nuovo testo: "<< lingue;
    stream.seek(0);
    stream << lingue << '\n';
    f.close();
    terza *t = new terza(linguaOrigine, linguaFinale);
    t->show();
    this->close();
    }
    }
    }
    @

    there is a problem. here we go! as the file is opened, the datas that occour to me are deleted. please, prove it and say me what is borrough. as for me it is a perfect code!



  • Sorry, I don't understand what's your problem. Can you rephrase it, please: What do you expect in your file, what do you get?



  • stream.readAll(); returns "", although the file contains a lot of strings.



  • Ah, of course. You open the file using mode QIODevice::Append - that will position the file "cursor" at the end, and of course there's nothing left to read ;-)



  • xD i am a "distrattone" (italian). i thank you a lot!! =)

    EDIT:
    @
    void cinque::writeNewWord(QString l1, QString l2, QString genere, QString wort, QString beispiel, QString parola, QString esempio)
    {
    if(wort.isEmpty() && beispiel.isEmpty() && parola.isEmpty() && esempio.isEmpty())
    {
    Prima *d = new Prima();
    d->show();
    this->close();
    }
    else
    {
    if(l1.compare("Deutsch") != 0 || l1.compare("Tedesco") != 0) genere = "";
    wort[0] = wort[0].toUpper();

        QFile f(QDir::currentPath() + "listaSezioni.txt");
        //aggiunta del nuovo biglietto al file listaSezioni.txt
        if(f.open(QIODevice::Text | QIODevice::ReadOnly))
        {
            QTextStream streami(&f);
            streami.seek(0);
            QString separatore = " | ";
            QStringList listaSezioni;
            QString riga;
            do {
                riga = streami.readLine();
                qDebug() << "riga: " << riga;
                if(riga.contains(l1) && riga.contains(l2))
                {
                    listaSezioni.append(riga);
                    listaSezioni.append(genere + separatore + wort + separatore + beispiel + separatore + parola + separatore + esempio + '\n');
                }
                else listaSezioni.append(riga);
            }while(!streami.atEnd());
            f.close();
    

    @

    ahm, 'xcuse me, what about here? riga = streami.readLine(); qDebug() << "riga: " << riga;
    output: "riga: "" "



  • Hard to say without knowing the actual contents of the file :)



  • the actual content is:
    @
    *Tedesco Italiano\n
    das Haus | das haus ist schon | casa | \n
    der Hund | | cane | \n
    *Inglese Italiano\n
    @



  • The code works for me.



  • here we go, dear Volker!!
    @
    QFile f(QDir::currentPath() + "listaSezioni.txt");
    @
    should be
    @
    QFile f(QDir::currentPath() + "/listaSezioni.txt");
    @

    "/" is the issue!! =)



  • Ah, that little typos - they are know to steal you a whole day of debugging - welcome to the club :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.