How can I read a txt file bottom up using cfloop? ...
# cfml-general
i
How can I read a txt file bottom up using cfloop? The below code reads it top to bottom. <cfloop index="line" file="#myfile#"> <cfoutput> The current line is #line# <br /> </cfoutput> </cfloop>
s
To my knowledge there is no way to start at the bottom. however https://docs.lucee.org/guides/cookbooks/loop_through_files.html provides a good guide for getting started, it is a more memory efficient way of getting to the bottom without loading the whole file in memory
👍 1
i
I found a way. Read the file and put it in an array and then loop the array bottom up. <cfloop index="i" from="#arrayLen(afilecontent)#" to="1" step="-1"> <cfoutput>#afilecontent[i]#</cfoutput><br> </cfloop>
👍 1
d
I figured, wrongly apparently, that you were tyring to avoid reading the whole file into memory at once. Without that restriction, what you've got should be fine.
i
Thanks - the file size is relatively very small and that it why.
s
I envisioned the same as @Dave Merrill since most times this question is asked about log files