Results 1 to 4 of 4

Thread: problem with reading input data in qt

  1. #1
    Join Date
    Mar 2007
    Posts
    16
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile problem with reading input data in qt

    hi, (first of all, i dont have a good english so hope u understand)
    i ve a prob with reading the data from a file, in fact i must create a multiple choice question program and i have finished the phase of writing to a file in this format.

    Geography
    2 //number of questions
    Which one is the capital of India?
    Paris
    0
    New Delhi
    1
    Frankfurt
    0
    Mumbai
    0
    Which one is the capital of France?
    Paris
    1
    Jakarta
    0
    New York
    0
    Kabul
    0

    and for readin this file , i have done something like this:

    QFile file (filename);
    if (!file.open(QFile::ReadOnly | QFile::Text))
    {
    QMessageBox::Warning(this, "Application","can not open the file");

    }

    QTextStream in (&file);
    QString mcqnameQString = in.readLine();
    string mcqnameString = mcqQString.toStdString(); // i change the QString to String //cause i transfer it to a c++ class that i ve created

    int nbQuestion;
    in >> nbQuestion;
    // i want to put 2 in nbQuestion and go to the newline but i think this method doesnot let me to go to the next line of Stream, and i dont want to use a readLine() methode cause, it returns the line as QString but i want it as integer


    quest = new Questionnaire(mcqnameString, nbQuestion); // my c++ class where i //should save all the stream that i read

    for (int i=0; i<nbQuestion; i++)
    {
    QString questionQString = in.readLine();
    string questionString = questionQString.toStdString();
    quest -> tabquestion[i].setQuestion(questionString);

    for (int j=0; j<4; j++)
    {
    QString responsQString = in.readLine();
    string responsString = responsQString.toStdString();
    quest -> tabquestion[i].tabrespons[j].setRespons(responsString);

    int state; // same probleme as for saving the nbQuestion mentioned
    in >> state; //on top
    quest -> tabquestion[i].tabrespons[j].setStateRespons(state);

    }
    }

    my problem is that doing this i get a segmentation error, i know the problem comes with the (in >> ) operator, cause i think after putting the data on the integer variables , it does not go to the next line as it is the case of the readLine() i guess. cause if i put all of them as readLine(), i does not get an error, but i dont want it like that, i want to have the integers as integer and the text as text...
    pls help me ..

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with reading input data in qt

    Hello,

    If you have everything text oriented, do this:

    Instead of :
    Qt Code:
    1. int nbQuestion;
    2. in >> nbQuestion;
    To copy to clipboard, switch view to plain text mode 

    do this:
    Qt Code:
    1. int nbQuestion = 0;
    2. QString strNumOfQuestions;
    3. in >> strNumOfQuestions;
    4. nbQuestion = strNumOfQuestions.toInt();
    To copy to clipboard, switch view to plain text mode 


    Do this for every integer you read from the stream.

    Regards

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with reading input data in qt

    Anyway, the problem was because int QTextStream:perator>>() was not going to the next line in case of "\n" or "\r\n" was encountered.

  4. #4
    Join Date
    Mar 2007
    Posts
    16
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with reading input data in qt

    Hey tnx a lot for the the post,
    in fact , i havent tried yet ur solution, but for meanwhile i putted an String varialbe after reading each integer which reads the \n and pass to next line.. and this has solved my problem

    QString nothing;
    nothing.readLine();


    but i know i should pass to a better method.

Similar Threads

  1. Transferring data input from a Widget to a cpp file
    By Ahmad in forum Qt Programming
    Replies: 1
    Last Post: 10th March 2007, 09:59
  2. reading DVD data and mpeg2 streams
    By guestgulkan in forum General Programming
    Replies: 1
    Last Post: 18th February 2007, 22:24
  3. reading and writing data from a QTableWidget
    By zorro68 in forum Qt Programming
    Replies: 4
    Last Post: 29th January 2007, 20:51
  4. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  5. scanf not reading input correctly
    By jamadagni in forum General Programming
    Replies: 1
    Last Post: 8th January 2006, 15:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.