Traiter les fichiers textes


Nous allons décrire ici comment utiliser les fichiers textes pour faire une page tips of the days qui récupère aléatoirement une ligne dans un fichier texte.

default.asp secret.asp
<html>
<head>
<title>Texte</title>
</head>

<%
Set objFile = Server.CreateObject("Scripting.FileSystemObject")
Set objStream = objFile.OpenTextFile(Server.MapPath("/tutorial") & "\texte\tips.txt")

Randomize Timer
intLine = Int(Rnd * 10)
For i = 0 to intLine
objStream.SkipLine
Next

strText = objStream.ReadLine

objStream.Close
Set objStream = Nothing
Set objFile = Nothing
%>

<h2 align="center"><font color="#0000FF">S'essayer avec un fichier Texte</font></h2>

<hr>

<%=strText%>

</body>
</html>

Debut de page




Déclaration de la variable objFile


Initialise le compteur
défini le ligne à afficher : intline
Boucle pour sauter les lignes avant intline


strText définie la ligne à afficher

Ferme l'object objFile






Affichage de la ligne


Téléchargez le code

[Précédent] [Retour] [Suite]