[Solved] Same code not working if used as a function from a different method
-
Hi!
I have a class called Widget and two methods in it called methoda and methodb.
I have a code snippet which works fine in methoda.In pseudo code format it looks like this:
@
while not end_of_file
in: data from file
…
end while
@If I put this code into methodb and call it from methoda like a function it does not work because the condition will be false (even if I position to the start of the file before the cycle). Every variable, methoda and methodb are public. What is the problem?
-
It's very difficult to guess what's wrong with the code without actually having it - pseudo code doesn't do it.
Maybe you are referencing to uninitialized variables or the like. Could you provide some actual code on this?
-
Of course. Basically I need to fill a record structure with data from a file and get the number of records in that file. Normally it works but in this case Qt thinks it reached end of file instantly.
@
pchar=data5;i=0;*(pchar+4)=0;file2.seek(0);
// if (struct_IN()==-1) //not good
// return;
@@
int Widget::struct_IN() {
//OUT: i=number of records
while (file2.pos()<file2.size()) {
stream2.readRawData(pchar,4);
rekord[i].ID = QByteArray(pchar);
stream2.readRawData(pchar,4);
rekord[i].Offset = QByteArray(pchar);
stream2 >> uiSzovH;
rekord[i].length = uiSzovH;
rekord[i].string=(char *)calloc(uiSzovH,sizeof(char));
if (!rekord[i].string) {
QMessageBox::information(this, tr("Error!"), tr("Memory allocation error."));
felSzabadit3();
return (-1);
}
stream2.readRawData(rekord[i].string,uiSzovH);
i++;
}
return 0;
}
@ -
I assume file2 is a subclass of "QIODevice":http://qt-project.org/doc/qt-4.8/qiodevice.html
In that case you could use "QIODevice::atEnd()":http://qt-project.org/doc/qt-4.8/qiodevice.html#atEnd instead of pos() and size().
I don't know if you set the current position in the file correctly before using "QIODevice::pos()":http://qt-project.org/doc/qt-4.8/qiodevice.html#pos as well as I'm not sure if the position is usually set to the beginning of the file. If not this may be the reason for your loop condition to be not true. So maybe something like this might work for you:
@
int Widget::struct_IN() {
file2.seek(0);
...
}
@ -
Maybe you need to make sure the file can be opened or maybe seek() gives a false as return value. If the seek was successful you will get a true. Maybe check if first. Maybe if file2 is a QFile class you may want to (as Daniel suggested) use the QIODevice. QFile inherits this and they make life a little simpler when writing data to and from a file.
greetz -
file2 is a QFile object and yes, I had to open and redeclarate (!) it. What I don't understand is why can I use simple int variables without redeclarating them in Widget::struct_IN() but not this QFile object?
-
if the QFile file2 is in your class definition it should remain the same if the class still exists, so you need only to open it once! If the file is still open you are unable to open it again, but you start to confuse me now. What is the real problem??
Btw if you use known byte length in that structure you can just pass then directly into the variable without the ReadRawData function. so:
@
stream2 >> rekord[i].ID >> rekord[i].Offset;
@
If the rekord[i].ID is a int (so probably 4 bytes) the >> operator will read 4 bytes because the variable that receives the bytes is 4 bytes long. If rekord[i].Offset is maybe a byte, the >> operator will read 1 byte and so on. -
The real problem is what I mentioned in my first post. Calling this code snippet as a function makes the QFile object (and probably the record too) unavailable. I tried passing the addresses as function parameters so I could handle them with pointers but I couldn't figure out the correct syntax.
Oh, the ID's are 4 bytes long but they are uint32 numbers with different endianess that's why I chose to store them in QByteArray types.
-
The only thing that comes to mind is that you want to use the QFile outside the class or scope that it is declared for. Maybe with more code we could be of more help.
-
I think that is the problem. Too bad. Thanks anyway.