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!

reading from a text file by line

Status
Not open for further replies.

marknav

Programmer
Sep 26, 2002
14
0
0
US
Hi
I need to read information from a text file.
I need to find the line when a certain name appears and
print this line.
I already did the first part, when i read the information from the file to a parameter, and than by the find function i found the position of the name inside the line.
My problem now is :
How do I take the whole line, I cannot use fixed number of character because the lines' size vary.
I was thinking to use the breaks in lines chr(13)&chr(10)
to specify where a line begin and where it ends.
But I don’t know how. Any suggestions?
Thank you.
 
What I would do is treat the file as a list, with an endline as a delimiter:

<cfloop list=&quot;#yourfile#&quot; index=&quot;line&quot; delimiter=&quot;#chr(13)#&quot;>
<cfif Find(name,line) GT 0>
<cfoutput>#line#</cfoutput>
<cfbreak>
</cfif>
</cfloop>
 
Once you have the file content in a variable, you can just treat the variable as a list, with Chr(13) as the delimiter.

Something like:

Code:
<cffile action=&quot;READ&quot; file=&quot;myfile.txt&quot; variable=&quot;filecontent&quot;>

<cfset foundonline = ListContainsNoCase(&quot;#filecontent#&quot;, nametomatch, Chr(13))>

<cfif foundonline gt 0>
   <cfoutput>#ListGetAt(filecontent, foundonline, Chr(13))#</cfoutput>
</cfif>

or, if you expect that you'll get more than one hit per name, you'd have to do something like:

Code:
<cffile action=&quot;READ&quot; file=&quot;myfile.txt&quot; variable=&quot;filecontent&quot;>

<cfloop index=&quot;whichline&quot; list=&quot;#filecontent#&quot; delimiters=&quot;#Chr(13)#&quot;>
  <cfif FindNoCase(nametomatch,whichline)>
     <cfoutput>#whichline#<br /></cfoutput>
  </cfif>
</cfloop>

Hope it helps,
-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top