Login | Register | FAQ
Anonymous

Lesson 15: Reading from a text file

In the previous lesson, we learned how to use the DocumentationFileSystemObject to access the server's filesystem. In this lesson, we will use this learning to read from an ordinary text file.

Text files can be extremely useful for storing various kinds of data. They are not quite as flexible as real databases, but text file typically don't require as much memory. Moreover, text files are a plain and simple format that works on most systems.

Open the text file

We use the DocumentationOpenTextFile method to open a text file. The syntax is as follows:


	Object.OpenTextFile(filename, mode)
	
	
filename
Name of the file to be opened.
Mode
Mode can set to 1 (ForReading), 2 (ForWriting) or 8 (ForAppending). In this lesson we will only read from a file and, therefore, use ForReading. In the next lesson, we will learn to write to a file.

The examples in this lesson use the text file unitednations.txt. This is a simple list of the Programmes and Funds of the United Nations and their domains. You can either download the file, or you can create your own file and test the examples with it.

First, let's try to open unitednations.txt:


	<%
	' Variables
	Dim fso, f filespec

	' FileSystemObject
	Set fso = CreateObject("Scripting.FileSystemObject")

	' Find the physical location of the text file
	filespec = Server.Mappath("/tutorials/asp/unitednations.txt")

	' Open the text file
	Set f = fso.OpenTextFile(filespec,1)


	' Close the text file
	f.Close
	%>

	
	

Example 1: Read a line from the text file

With the method Documentationreadline, we can read a line from the text file. This method reads until the first line break (but not including the line break).


	<html>

	<head>
	<title>Reading from text files</title>
	</head>
	<body>
	<%
	Dim fso, f, filespec

	Set fso = CreateObject("Scripting.FileSystemObject")
	filespec = Server.Mappath("/tutorials/asp/unitednations.txt")

	Set f = fso.OpenTextFile(filespec,1)

	' Read line from the text file and write the contents to the client

	Response.write f.readline 

	f.Close
	%>
	</body>
	</html>
	
	

Example 2: Read all lines from the text file


	<html>

	<head>
	<title>Reading from text files</title>
	</head>
	<body>
	<%
	Dim fso, f, filespec

	Set fso = CreateObject("Scripting.FileSystemObject")
	filespec = Server.Mappath("/tutorials/asp/unitednations.txt")

	Set f = fso.OpenTextFile (filespec,1)

	' Read line by line until end of file

	Do While Not f.AtEndOfStream 
	   Response.write f.ReadLine & "<br />" 
	Loop 

	f.Close

	%>

	</body>
	</html>
	
	

In this case we chose to loop through all the lines, but we could have achieved the same result with the method DocumentationReadAll . If you work with very large text files, be aware that the ReadAll method uses more resources than the ReadLine method. For smaller files, it makes very little difference.

Example 3: Skip lines

You don't need to read all lines in a file. With the method DocumentationSkipLine, you can skip lines.

This is shown in the example below, where the first 5 lines in the file are skipped and whereafter the sixth line is read:


	<html>
	<head>
	<title>Reading from text files</title>

	</head>
	<body>
	<%
	Dim fso, f, filespec

	Set fso = CreateObject("Scripting.FileSystemObject")
	filespec = Server.Mappath("/tutorials/asp/unitednations.txt")
	Set f = fso.OpenTextFile(filespec,1)

	' Skip the first five lines

	For intLine = 1 to 5 
	   f.SkipLine 
	Next 

	' Write the sixth line
	Response.write f.readline & "<br />" 


	f.Close

	%>
	</body>
	</html>
	
	

Example 4: A simple link directory

As mentioned at the beginning of this lesson, text files can be an excellent data storage. This is illustrated in the next example where we create a simple link directory from the contents of the text file unitednations.txt.

The file is systematically written with the name of the program, then a comma, and then the domain. As you can probably imagine, more information could easily be stored in this comma-separated data file.

To get the information in each line, we use an array. See Lesson 8 for more information on arrays.


	<html>
	<head>
	<title>Reading from text files</title>

	</head>
	<body>
	<%
	Dim fso, f, filespec

	Set fso = CreateObject("Scripting.FileSystemObject")
	filespec = Server.Mappath("/tutorials/asp/unitednations.txt")
	Set f = fso.OpenTextFile(filespec,1)

	' Read line by line until end of file

	Do While Not f.AtEndOfStream

	' Make array using comma as delimiter
	   arrM = Split(f.readline ,",") 

	' Write links - get the data in the array

	   Response.write "<li><a href='http://" & arrM(1) & "'>" & arrM(0) & "</a></li>" 

	Loop 


	f.Close
	%>
	</body>
	</html>
	
	

Quite handy, right? In principle, you could now just expand the text file with hundreds of links or perhaps expand your directory to also include address information.

In the next lesson, we will look at how to write to a text file.



<< Lesson 14: FileSystemObject

Lesson 16: Writing to a text file >>