Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, April 2007
Trying to read a log file not success on linux


Message 1 in thread

Hi all,

This code is successfull in windows (XP and  Qt-4.1), but reads just 
first time until reached en of file on Linux (Suse 10.1 and Qt-4.1).

Can anyone help me.



void MatchThread::run()
{
    QFile file("data.log");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
       qDebug()  <<  "Error reading:  " "data.txt");
       return;
    }
  
    QTextStream in(&file);
    while (true) {
        QString line = in.readLine();
        if(line.contains("80 00 00 68")){
            while (line != "") {
                line = in.readLine();
                if(line.contains("00 00 00 07")){
                     emit matchData(line.mid(53,7));
                }
            }     
        }
    }     
}

--
 [ signature omitted ] 

Message 2 in thread

Ramiro Arenas R. schrieb:
> Hi all,
> 
> This code is successfull in windows (XP and  Qt-4.1),

With "succesful" I assume you mean something like "After I have reached
the end of the file the reader thread (the Consumer) should continue to
read the file as soon as someone else (the Producer) has appended more
data to it.

> but reads just
> first time until reached en of file on Linux (Suse 10.1 and Qt-4.1).

The problem is probably that on Linux the lower file implementation is
such that once you pass the end of a file it is put into a bad state and
hence you can never read from it again, until you close and re-open it.
I would expect that this is also the behaviour on Win32, but obviously
it is not (according to what I think you mean by "succesful").

This is the canonical "Reader/Writer" (or Producer/Consumer) problem and
usually described in the 2nd chapter of every "Multithreading" school
book. Google for it (or even look into the Qt documentation about
multithreading!) and you will find a solution.

And maybe using a named pipe or shared memory is better suited than a file?

Cheers, Oliver

--
 [ signature omitted ] 

Message 3 in thread

On 12 apr 2007, at 17.07, Till Oliver Knoll wrote:

> Ramiro Arenas R. schrieb:
>> Hi all,
>>
>> This code is successfull in windows (XP and  Qt-4.1),
>
> With "succesful" I assume you mean something like "After I have  
> reached
> the end of the file the reader thread (the Consumer) should  
> continue to
> read the file as soon as someone else (the Producer) has appended more
> data to it.
>
>> but reads just
>> first time until reached en of file on Linux (Suse 10.1 and Qt-4.1).
>
> The problem is probably that on Linux the lower file implementation is
> such that once you pass the end of a file it is put into a bad  
> state and
> hence you can never read from it again, until you close and re-open  
> it.
> I would expect that this is also the behaviour on Win32, but obviously
> it is not (according to what I think you mean by "succesful").
	Any book on C and C++, will tell you that that after reading EOF from
a FILE * or an std::istream, the stream will always return EOF until  
reset.
	There are C and C++ calls to reset the EOF status of an open file
without closing and reopen it.
	You should probably do a stat on the logfile however, and not do  
reset EOF status
and resume reading until the logfile changes size. Otherwise your  
code might prevent
the computer from going to sleep when inactive. This is particularly  
important on portables.
>
> This is the canonical "Reader/Writer" (or Producer/Consumer)  
> problem and
> usually described in the 2nd chapter of every "Multithreading" school
> book. Google for it (or even look into the Qt documentation about
> multithreading!) and you will find a solution.
>
> And maybe using a named pipe or shared memory is better suited than  
> a file?
>
> Cheers, Oliver
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx  
> with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>

------
What is a woman that you forsake her, and the hearth fire and the  
home acre,
to go with the old grey Widow Maker.  --Kipling, harp song of the  
Dane women
Tommy Nordgren
tommy.nordgren@xxxxxxxxx



--
 [ signature omitted ] 

Message 4 in thread

Tommy Nordgren schrieb:
>>> ...
>>> but reads just
>>> first time until reached en of file on Linux (Suse 10.1 and Qt-4.1).
>>
>> The problem is probably that on Linux the lower file implementation is
>> such that once you pass the end of a file it is put into a bad state and
>> ...
>     Any book on C and C++, will tell you that that after reading EOF from
> a FILE * or an std::istream, the stream will always return EOF until reset.

That's true for FILE and std::istream. And that's what I was referring
to as "lower (level)" implementation, the stuff that QFile is actually
using (without looking at the actual implementation right now).

Depending on how QFile is implemented on Windows though it may indeed be
that you can still read again and again with readLine() (when new data
becomes available), even though you have passed EOF the last time
whereas on Unix the FILE (or std::istream or whatever) would get into a
bad state (until it is reset()).

At least that was my understanding the OP was observing on Windows... (I
did not verify it) and that would explain why he experienced different
behaviour on Linux and Windows.

Cheers, Oliver

--
 [ signature omitted ]