PDA

View Full Version : how to read only last line on a text file?


ph2003
10-20-2011, 12:23 AM
hi all,

how to read only last line of a log file?

because the log is always updating in realtime.
if i do a loop reading full log every time.
it gets slower and slower when the file get larger :(

currently im using this code to do loop for reading:
string $machineProcess = ("c:/process.log");
int $filelogid = ` fopen $machineProcess "r" ` ;
string $nextLine = ` fgetline $filelogid `;
while (size($nextLine) > 0) {
$nextLine = ` fgetline $filelogid `;
}
fclose $filelogid;


thanks in advances!

gtbull80
11-15-2011, 02:20 PM
You can use a separate variable to store the last line...
string $lastLine;
string $machineProcess = ("c:/process.log"); int $filelogid = ` fopen $machineProcess "r" ` ; string $nextLine = ` fgetline $filelogid `; while (size($nextLine) > 0) {
$lastLine = $nextLine;
$nextLine = ` fgetline $filelogid `; }
print $lastLine;
fclose $filelogid;
Hope this helps:)

bendingiscool
11-16-2011, 01:50 PM
In python..



fil = open("/blah/blah/blah/textFile.txt","r")
lineList = fil.readlines()
fil.close()

print lineList[-1]

CGTalk Moderation
11-16-2011, 01:50 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.