PDA

View Full Version : problem with reading input data in qt



Ahmad
8th April 2007, 13:05
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 .. :confused:

marcel
8th April 2007, 18:16
Hello,

If you have everything text oriented, do this:

Instead of :


int nbQuestion;
in >> nbQuestion;

do this:


int nbQuestion = 0;
QString strNumOfQuestions;
in >> strNumOfQuestions;
nbQuestion = strNumOfQuestions.toInt();



Do this for every integer you read from the stream.

Regards

marcel
8th April 2007, 18:18
Anyway, the problem was because int QTextStream::operator>>() was not going to the next line in case of "\n" or "\r\n" was encountered.

Ahmad
9th April 2007, 10:58
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. ;)