×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    21

    reading file. i can not understand where is the error...

    excuse me, return to QString::section(): i am not able to use it.
    could you write any examples, please?

    if i have: "hello. i am a boy and i am 15 years old", i want "i am 15 years old".
    Last edited by sirspode; 2011-06-05 at 19:36. Reason: my mystake...

  2. #2
    Nokia Developer Champion
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: reading file. i can not understand where is the error...

    Looks to me like you read the entire file around line 3. Nothing left to read.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    21

    Re: reading file. i can not understand where is the error...

    'scuse me. i have just edited my thread.. =)

  4. #4
    Nokia Developer Champion
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: reading file. i can not understand where is the error...

    1) What are you going to do with "in"?
    2) Where is the rest of the function?
    3) Have you used QFileInfo to query the file to be sure it's really there and contains bytes?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    21

    Re: reading file. i can not understand where is the error...

    Code:
    void FileTrenner::trennen()
    {
        if(pathInput->open(QFile::Text | QFile::ReadOnly))
        {
            QTextStream in (pathInput);
            qDebug() << "pathInput vale: " << pathInput->fileName() << " pathOut vale: " << pathOutput->fileName();
            QFileInfo info (pathInput->fileName());
            if(info.size() != 0)
            {
                qDebug() << "canread != \"\"";
                if(pathOutput->open(QFile::Text | QFile::WriteOnly))
                {
                    qDebug() << "pathOut aperto";
                    QTextStream out (pathOutput);
                    //programma vero e proprio: codice fondamentale
                    int somma = 0;
                    while(pathInput->canReadLine())
                    {
                        QString line = pathInput->readLine();
                        qDebug() << "line vale: " << line;
                        if(somma + line.length() == sb_value)
                        {
                            out << line;
                        }
                        else if(somma + line.length() < sb_value)
                        {
                            somma = somma + line.length();
                            out << line;
                        }
                        else //somma + line.length() > sb_value
                        {
                            QString avanzo = NULL;
                            int numeroAltriCharsSuRiga = sb_value - somma, i = 0;
                            do
                            {
                                i--;
                                avanzo = line.section(" ",-1, i);
                            }while(avanzo.length() >= numeroAltriCharsSuRiga);
                            qDebug() << "i vale: " << i << "avanzo vale: " << avanzo;
                            somma = avanzo.length();
                            out << line.replace(avanzo, "\n");
                            out << avanzo;
                        }
                    }
                    //FINE programma vero e proprio: codice fondamentale
    
                    out << endl << endl << "Grazie per" << endl << "avere utilizzato" << endl << "File Trenner!";
    
                    resize(600,200);
                    l3 = new QLabel("Formattazione completata!\nGrazie per avere usato File Trenner! =)");
                    l3->setAlignment(Qt::AlignHCenter | Qt::AlignJustify);
                    lv->addWidget(l3);
                }
                else
                {
                    QMessageBox alert;
                    alert.adjustSize();
                    alert.move(500,300);
                    alert.setWindowTitle("File output: apertura");
                    alert.setText("Problemi nell'apertura del file di output.\nProvare a rieseguire il programma come amministratore!");
                    alert.exec();
                }
            }
            else
            {
                QMessageBox alert;
                alert.move(500,300);
                alert.adjustSize();
                alert.setWindowTitle("File input: vuoto");
                alert.setText("Il file di input non contiene caratteri.\nRiavviare il programma e scegliere un altro file!");
                alert.exec();
                pathInput->close();
            }
            pathInput->close();
            pathOutput->close();
        }
        else
        {
            QMessageBox alert;
            alert.move(500,300);
            alert.adjustSize();
            alert.setWindowTitle("File di input: apertura");
            alert.setText("Problemi nell'apertura del file di input.\n"
                          "Provare a eseguire il programma come amministratore\n"
                          "e verificare l'esistenza fisica del file di input!");
            alert.exec();
            pathInput->close();
            pathOutput->close();
        }
    }
    i just know that QFile::pathInput is unreadable...
    i do not know what have i to do with in...
    Last edited by sirspode; 2011-06-05 at 19:36.

  6. #6
    Nokia Developer Champion
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: reading file. i can not understand where is the error...

    So what happens? Is info.size() nonzero??

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    21

    Re: reading file. i can not understand where is the error...

    yes, it is! if i sum "pathInput.readLine();", the machine returns NULL ("")...

  8. #8
    Nokia Developer Champion
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: reading file. i can not understand where is the error...

    Is pathOutput by any chance addressing the same file as pathInput?

    Have you tried writing a SIMPLE program to just read the file and print it?

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    21

    Re: reading file. i can not understand where is the error...

    so, here the problem is:
    how to use QString::section()? examples in the documentation are unuseful for me...

  10. #10
    Nokia Developer Champion
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: reading file. i can not understand where is the error...

    Well, there are three version of section(). You first need to settle on which one you're going to use -- character separator, string separator, or regular expression separator. Otherwise the function is similar among the three, and the examples for the charactor separator version apply to all three in terms of what the index values mean.

    I find that the examples should make it reasonably clear, even though it's hard to explain without examples. What specifically can you not understand?

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    21

    Re: reading file. i can not understand where is the error...

    thank you!

Similar Threads

  1. Replies: 8
    Last Post: 2009-05-04, 08:13
  2. Replies: 9
    Last Post: 2009-04-21, 05:31
  3. Replies: 4
    Last Post: 2008-09-25, 12:02
  4. Can not understand, it is os or program's bug
    By eroslin in forum Symbian
    Replies: 1
    Last Post: 2008-07-03, 12:27
  5. J2me App can get a file which is not included in the Jar file??
    By skevelim in forum Mobile Java General
    Replies: 1
    Last Post: 2005-07-19, 03:40

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •