Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to read a text file and display it to the page

Status
Not open for further replies.

Keendev

Technical User
Jan 23, 2006
106
0
0
PH
Hi,

I just want to ask if this is possible,

I have a text file and I need to read starting from
line 15 up to line 32 of the text file.

Currently Im using the cffile="read" but it reads all of the contents of the file. Can I know if CF can do this ( specify starting line # and ending line number.

Thanks in advance.
 
AFAIK, there is no built in mechanism to loop from line X to Y. But you could use <cfloop file="c:\yourFile" ...> along with your own line counter variable to achieve this. (Assuming you are using CF8)


----------------------------------
 
If your file has line breaks in it for each line you can use them as delimiters. You can loop through the lines/positions you want.
<cffile action="read" file="C:\Myfile.txt" variable="FileVar">
<cfloop index="lineVar" list="#FileVar#" delimiters="#chr(10)#">
<cfoutput>#lineVar#</cfoutput>
...

chr(10) should be your line break.

Sorry for writing this in a hurry, but it should get you going in the right direction.
 
Actually, you could use a listgetat() function to get the values at a certain position using the end of line delimiters. You could use a loop to iterate through the file.
Code:
<cffile action="read" file="C:\Myfile.txt" variable="FileVar">
<cfloop index="i" From="15" to="35">
  <cfoutput>
   #LISTGETAT(FileVar, i, "chr(10)")# <BR>
  </cfoutput>
</cfloop>
"i" will be the index value as it loops from 15 to 35.

If your file has no delimeters for line breaks that could pose a problem, but still could be doable just a lot more work.

Hope this helps
 
I'm not sure what you mean by a file loop. This way you only hit the 20 records you want instead of looping through the whole file (we don't know how big this file is). This way you don't need the counting and logic it is built into the loop.

KEENDEV you out there?

BTW, cfsearching took a look at your blog, looks like a lot of good info. I wish I had more time to look around.
 
CF8 has a "file" attribute which allows you to loop through a file "by line". It is similar to the listGetAt approach, but with a few differences.

1) <cfloop file=".."> does not read the entire file into memory as <cffile..> does. That can be a performance an advantage with larger files.

2) List functions ignore empty elements. So if a file contains blank lines which should be accounted for, using list functions may return the wrong results.

Since items #1 and #2 are unknown at this point (and the line numbers needed are practically at the beginning of the file anyway) that is why I suggested a file loop. Of course if they needed lines 3000 through 3035, then a different approach would be better ;)

So I guess the bottom line here is, the best approach really depends on information we do not have at this point ;-)


----------------------------------
 
Ah, I see. We haven't made the move CF8 yet. We do lots of file/txt manipulation. I may need to push a little harder for the upgrade. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top