Unsolved Is there a direct way to know the lines of a text file ?
-
@RonaldViscarraL
The trouble is, the approach there which supposedly is the quickest is simplydata = readFile.readAll();
. If the OP here says he has triedreadAll()
and it's too slow for his size file, it doesn't really solve.@cyberpunker
You are usingstream.readLine( )
. You could use somestream.read( )
and do your own line counting, there might be an improvement there.Just how big/how many lines is your log file going to grow to? Have you actually tried any realistic testing of the read delay?
-
The api : QString QTextStream::readLine(qint64 maxlen = 0) ?
I also try to use a thread to deal with the text file , then send the readlines to the QTextEdit by SIGNAL~SLOT , and it seems very clumsy.
-
@cyberpunker
I am suggesting you might try not usingQTextStream::readLine()
, and insteadQTextStream::read()
orQTextStream::readAll()
, plus then do your own in-memory searching for\n
s to count lines. The theory is that may be faster thanQTextStream::readLine()
s. Frankly I'm dubious how much difference it might make, but you could give it a go.I did ask you just how big this file is, in bytes & lines? Have you actually tried some code with timings?
Your original question was, is it possible to know where a line is in a text file without reading each line, and the answer is, no it is not.
So, to prevent GUI freeze you are left with either doing something in a separate thread, or doing something where the user scrolling causes the file to be read in incrementally. at that time.
-
hi,
@JonB said in Is there a direct way to know the lines of a text file ?:
original question was, is it possible to know where a line is in a text file without reading each line
on windows you can use FINDSTR command with qprocess
FINDSTR /N "the word i search" file.txt
-
@LeLev
That just reads each line in the file, so why should that be any more efficient than, say,QTextStream::readAll()
, which the OP is saying is/might be too slow? -
@JonB its just an option. and because is is build in windows i thought it could be little faster somehow
-
In fact , it is the glog file , the file size is variable , I want to parse the log file and show its content according to a time period. The time range may may spans several log files , and the start time or stop time can be any position of the log file . So I need a mechanics which is efficecy , resource save, and human kind .
-
@cyberpunker
So I have given you the options. You will have to read all of the log file(s) in order to accomplish this, there is no shortcut. Have you looked at/answered where I keep asking you " Have you actually tried some code with timings?"? -
@cyberpunker To reinterpret your question, you want to know when another application appends another line to datum.txt? Perhaps QIODevice::waitForReadyRead() may be one way to determine when datum.txt changes size?
[https://doc.qt.io/qt-5/qiodevice.html#waitForReadyRead](link url) -
@Psnarf
I have not tried it out, but do you have any evidence thatQIODevice::waitForReadyRead()
will be triggered if another process writes to a file which this process has open? My suspicion is that it will not, but I could be wrong. I would expect to have to use https://doc.qt.io/qt-5/qfilesystemwatcher.html#fileChanged if I wanted to be notified of a file content change from an external program.