Qt-jambi-interest Archive, November 2006
Problems with QTextStream.readLine() and QFile.atEnd()
Message 1 in thread
hello
I've detected some problems using QTextStream in QtJambi. The source
works fine in C++, but translating to Java behaves different.
Im reading several lines from a configfile. After the first
{QTextStream} textstream.readLine()
the method
{QFile} configFile.atEnd()
returns true, but there are several lines in the file. As I said, the
source works fine in C++, but I'm not very familar with Java, so maybe I
overlooked some details.
best Regards
Arne
the complete source (in Java):
// ... Method for open the ConfigFile
static boolean LoadConfig(String file)
{
// the default name should be "lux.cfg"
// remember the name of the configfile
m_file = file;
if (m_file.length() == 0) {
m_file = TEXT_LUX_CFG;
}
QFile configFile = new QFile(QApplication.applicationDirPath()
+ "/" + m_file);
// open the file
if (configFile.open(QIODevice.OpenModeFlag.ReadOnly) == true)
{
String zeile = "";
QTextStream textStream = new QTextStream(configFile);
// there are several lines in the file !!
while (configFile.atEnd() == false)
{
// try to get the key that means until the first '='
zeile = textStream.readLine();
gleichPos = zeile.indexOf('=');
// if there ist a '='
if (gleichPos > 0)
{
// key = text before '='
String key = zeile.substring(0,gleichPos);
// content = text after '='
String content = zeile.substring(gleichPos + 1);
// remove blanks at beginning and end
key.trim();
// to upper case
key.toUpperCase();
// remove blanks at beginning and end
content.trim();
// if there ist a valid key and a valid content
if ( (key.length() != 0) &&
(content.length() != 0) )
{
// key to keylist
m_keyList.add(key);
// content to contentlist
m_contentList.add(content);
}
}
}
// there ist a configfile
m_isConfigFile = true;
// no need to save current configparams
m_change = false;
// close the file
configFile.close();
}
return m_isConfigFile;
}
Message 2 in thread
Arne Stocker wrote:
> hello
>
> I've detected some problems using QTextStream in QtJambi. The source
> works fine in C++, but translating to Java behaves different.
>
> Im reading several lines from a configfile. After the first
>
> {QTextStream} textstream.readLine()
>
> the method
>
> {QFile} configFile.atEnd()
>
Hi, Arne.
When the text stream opens on a file it may read some or all of the data
in the file into an internal buffer. For this reason, QTextStream has
its own atEnd() implementation which takes this into account.
By replacing "configFile.atEnd()" with "textStream.atEnd()" in your
code, it should work fine.
Hope this helps!
-- Eskil
Message 3 in thread
He Eskil
thank you for the hint. I would have never seen this, because in c++
everything works fine (that doesn't mean I would not change the source
in c++ :-)
best regards
Arne
Message 4 in thread
Arne Stocker wrote:
> He Eskil
>
> thank you for the hint. I would have never seen this, because in c++
> everything works fine (that doesn't mean I would not change the source
> in c++ :-)
Hi Arne,
Is the C++ code based on Qt 4.2 as well, or a different version of Qt?
-
Gunnar
Message 5 in thread
> Is the C++ code based on Qt 4.2 as well, or a different version of Qt?
Hello Gunnar
it's based on Qt 3.3.6, so maybe it would not work on Qt 4.2 either (I
did not check yet).
Arne
Message 6 in thread
Arne Stocker wrote:
>> Is the C++ code based on Qt 4.2 as well, or a different version of Qt?
>
>
> Hello Gunnar
>
> it's based on Qt 3.3.6, so maybe it would not work on Qt 4.2 either (I
> did not check yet).
That explains it ;-)
The buffering strategies have changed quite a bit in QIODevice, QFile
and QTextStream since Qt 3.3.
best regards,
Gunnar