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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to split file content to array

Status
Not open for further replies.

discusmania

IS-IT--Management
Oct 24, 2000
158
AP
Hi guys...

who know how to split content of the file into array, one line for becomes 1 array element. thanks so much.

Ron
 
Code:
<%@ Language=VBScript %>
<% Option Explicit %>
<% Response.Buffer = True %>
<%
dim i
dim m
dim objFileSys
dim theFile
dim lineTxt

set objFileSys = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
set theFile = objFileSys.OpenTextFile(Server.MapPath(&quot;./&quot;) & &quot;/filename.txt&quot;,1)

i = 0
WHILE NOT theFile.AtEndOfStream
  i = i + 1
  theFile.SkipLine
WEND
theFile.Close

dim arrLines(1 to i)

set theFile = objFileSys.OpenTextFile(Server.MapPath(&quot;./&quot;) & &quot;/filename.txt&quot;,1)
FOR m = 1 to i
  arrLines(m) = theFile.ReadLine  
NEXT
%>

Now you've got an array arrLines with each line from the text file in it.

The TextStreamObject method ReadLine, which you can see is being used to populate the array, automatically moves the file pointer to the next line so there's no nead to issue a SkipLine method.

ToddWW


 
Yes... i did the same way. But the results always give 26 lines (array elements) while the lines in the file is 37. what goes wrong?

thanks so much,
Ron
 
Not sure, I tested that code and it worked great. Try it on a homemade text file.

Are you using the example I provided which counts the lines in the file first to dim the array, then opens the text file again and persists each line into the array ?

ToddWW
 
Hi,

Plaese try the following simple one

<%

' your ' file path to read from
data_file=&quot;d:\

set fsObj=server.createObject(&quot;scripting.filesystemObject&quot;)

set fileObj =fsObj.OpenTextFile(data_file)
dim line_array()
line_limit=100
Redim line_array(line_limit)
howmany=1
fileObj.readline
while not fileObj.atendofstream
line_array(howmany)=fileObj.readline
'response.write line_array(howmany) & &quot;<br>&quot;
howmany=howmany+1

wend
%>

the line_array(howmany-1) will give you the content of the file in the array. you can set the line_limit according to your need.

Please let me know it helped
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top