PDA

View Full Version : qstringlist output to file



martial_arts_drummer
27th February 2011, 18:10
ok, I have a QStringList that I am attempting to write to a file

Read_Request_Time.at(i) = "109,120,085";


if (Read_Request_Time.at(i).size() > 0)
out << "Read_Request" << "\t" << Read_Request_Time.at(i) << "\n";

the write causes a runtime error and crashed the application.

I am opening the file correctly, other writes succeed.

What is wrong with this code? I don't see an error?

thanks

John

Zlatomir
27th February 2011, 18:21
I think that your using incorrectly the at() member function (at(i) returns a const T < in out case is a const QString> so you can't assign to that)

But post more code, so that we can see what is wrong.

stampede
27th February 2011, 18:32
Make sure that i is in range 0 <= i < list.count() , you'll get failed assertion otherwise:

inline const T &QList<T>::at(int i) const
{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range");

martial_arts_drummer
27th February 2011, 18:42
i is in range of the QStringList

Read_Request_Time.at(i) = "123,456,789"

Opening the file for writing:

QFile fp(fileName);
if (!(fp.open(QIODevice::WriteOnly|QIODevice::Text)))
exit(-1);

QTextStream out(&fp);

then I am doing:

if (system_name.at(i).size() > 0)
out << "System_Name" << "\t" << system_name.at(i) << "\n";

with numerous QStringLists SUCCESSFULLY!! ???

until I get to:

if (Read_Request_Time.at(i).size() > 0)
out << "Read_Request" << "\t" << Read_Request_Time.at(i) << "\n";

this write cause an application error and crashes. and Read_Request_Time.at(i) does contain valid data. "123,456,789"

Curious!!

stampede
27th February 2011, 18:58
Curious!!
More curious for me is how did you manage to compile this one:

Read_Request_Time.at(i) = "123,456,789"
I can't compile this code ( as expected, because QList::at returns non-assignable const QString& ):

QStringList list;
list.append("a string");
list.at(0) = "other string"; // error: passing 'const QString' as 'this' argument of 'QString& QString::operator=(const char*)' discards qualifiers



Read_Request_Time.at(i) does contain valid data. "123,456,789"
How do you know that ? Maybe post more code.

martial_arts_drummer
27th February 2011, 19:16
I did not insinuate that my snippet of code was compilable.

The QstringList Read_Request_Time contains "123,456,789" at the ith location

as seen by using a debugger by listing the contents of:
Read_Request_Time.at(i)

This is not how I loaded data, I was trying to let you know what the variable was.

Not sure how much more code is needed.
I show how I open the file
I show how I am writing data to file successfully.
I showed where it is failing and
I showed the contents of the variable when it crashes.

Never mind. I will figure it out myself.

stampede
27th February 2011, 19:27
I did not insinuate that my snippet of code was compilable.

The QstringList Read_Request_Time contains "123,456,789" at the ith location

as seen by using a debugger by listing the contents of:
Read_Request_Time.at(i)

This is not how I loaded data, I was trying to let you know what the variable was.
But will you agree that all above informations were missing from previous posts ? That's why we ( I think Zlatomir also ) were confused and focused on wrong part of your code.
Now we know more.
If you still want us to help, try this one to know if it's really the write that causes errors:

if (Read_Request_Time.at(i).size() > 0){
out << "Read_Request" << "\t" << "123,456,789" << "\n";
}
or another way, see what debugger says about this one:

if (Read_Request_Time.at(i).size() > 0){
const QString str = Read_Request_Time.at(i) ;
out << "Read_Request" << "\t" << str<< "\n";
}

martial_arts_drummer
27th February 2011, 19:54
I don't agree that the information was missing, but I will agree that the message may have been less than concise.

But anyhow, I already tried hardwiring the varaiable ala.

if (Read_Request_Time.at(i).size() > 0)
out << "Read_Request" << "\t" << "123,456,789" << "\n";

What is interesting is that I changed it to store a local QString ala.

if (Read_Request_Time.at(i).size() > 0)
{ <==========
QString temp_string = Read_Request_Time.at(i);
out << "Read_Request" << "\t" << temp_string << "\n";
}

What happens here, is that the app crashes on the first curly bracket (I marked it with <====)

I then thought about changing the input if statement to:

if (Read_Request_Time.size() > i) // this is a for loop and i increments.
{
QString temp_string = Read_Request_Time.at(i);
out << "Read_Request" << "\t" << temp_string << "\n";
}

with the same behavior. Very curious.

thanks

John

stampede
27th February 2011, 20:19
But anyhow, I already tried hardwiring the varaiable ala.

if (Read_Request_Time.at(i).size() > 0)
out << "Read_Request" << "\t" << "123,456,789" << "\n";
And it crashed or not ?

I really want to help, but its difficult to do so as I only see code snippets out of context, which should be ok under some default assumptions ( like: i is local variable in some kind of loop, that cannot be modified elsewhere ( same for Read_Request_Time list ), i is in valid range, only one thread is accessing them at a time ect... ).
Do you use threads in you code ?
By looking just at those snippets it's difficult to tell whats wrong, because they look ok.

martial_arts_drummer
27th February 2011, 21:05
yes, it crashed.

Hence my decision to change the conditional statement from

if (Read_Request_Time.at(i).size() > 0)

to

if (Read_Request_Time.size() > i)

and it crashed on the first curly bracket.

No threads. i is a local variable, I am able to see the contents of Read_Request_Time.at(i) from within a debugger and it contains valid data.

Added after 40 minutes:

I will post here when I find the issue. I see nothing obvious that would cause this error.

thanks

John