...

View Full Version : writing to a file



spetsnaz
07-05-2004, 11:49 AM
hi. iam new to the whole asp thing. i want to make a way of writing to a text file. here is the input:



<html>
<head>
<title>text epriment input</title>

<body>
<form name="form" method="get" action="text.asp">
<input type="text" name="textfield">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>


and the proccessing bit:


<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<html>
<head>
<title>test experiments output</title>
</head>
<body>
<%
input = request.QueryString("textfield")

set fs = Server.CreateObject("Scripting.FileSystemObject")
set f = fs.OpenTextFile(Server.MapPath("text.txt"),true)
f.Write(input)
response.Write(f.ReadAll)
f.close

f = nothing
fs = nothing

%>

</body>
</html>


aparently iam using the wrong file mode, but iam using the overwrite mde, ie 1. why doesnt it work?

thnx for replies.

PS iam using asp not asp.net! :eek:

Roelf
07-05-2004, 12:09 PM
You have to specify an io-mode for the filehandle, this would be the second argument for the method

See here for info about OpenTextFile method (http://www.devguru.com/Technologies/vbscript/quickref/filesystemobject_opentextfile.html)

spetsnaz
07-05-2004, 11:57 PM
thnx for the speedy reply, what was 8 (constant for appending) from:

set f = fs.OpenTextFile(Server.MapPath("text.txt"),true)

it should have been:

set f = fs.OpenTextFile(Server.MapPath("text.txt"),8,true)

i have a follow on question from the above. i will post it tommorow...

Bullschmidt
07-06-2004, 12:17 AM
And in the meantime here's a good FSO resource:

FileSystemObject
http://www.4guysfromrolla.com/webtech/faq/faqtoc.shtml#FileSystemObject
5-part FAQ

spetsnaz
07-06-2004, 01:28 PM
ok. what i have is this menu screen:


<body>
menu<br>
<a href="textregistration.htm">register</a><br>
<a href="textlogin.htm">log in</a><br>
</body>


the textregistration.htm is this:


<body>
registration<br>
<form name="form" method="get" action="textinput.asp">
<input type="text" name="textfield">
<input type="submit" name="Submit" value="Submit">
</form>
</body>


the textlogin.htm is this:


log in<br>
<form name="form" method="get" action="textoutput.asp">
<input type="text" name="textfield">
<input type="submit" name="Submit" value="Submit">
</form>
</body>


the textinput.asp:


<body>
<%
dim fs,f,input
input = request.QueryString("textfield")

set fs = Server.CreateObject("Scripting.FileSystemObject")
set f = fs.OpenTextFile(Server.MapPath("text.txt"),8,true,-2)
f.WriteLine(input)
f.close

set f = nothing
set fs = nothing

%>
return to <a href="menu.htm">menu</a>
</body>


and the thing which is causing the problem, the textoutput.asp:


<body>
<%

lines = 1
''''''''''''''''''''''''''''''''''''''''''''''''''set up all variables
input = request.QueryString("textfield")
set fs = Server.CreateObject("Scripting.FileSystemObject")
set f = fs.OpenTextFile(Server.MapPath("text.txt"),1,true,-2)

''''''''''''''''''''''''''''''''''''''''''''''''''output contents for manual check
Response.Write(f.ReadAll)

''''''''''''''''''''''''''''''''''''''''''''''''''count the number of files
do while f.AtEndOfStream = false
lines = lines + 1
loop

''''''''''''''''''''''''''''''''''''''''''''''''''check for username
for x=1 to lines
if f.Read(x) = input then
a = true
else
a = false
next x

''''''''''''''''''''''''''''''''''''''''''''''''''infor user of his/her registration statues
if a = true then
Response.Write("your are registered")

else
Response.Write("you are not registered")

''''''''''''''''''''''''''''''''''''''''''''''''''prevent memory leaks
f.close
set f = nothing
set fs = nothing
%>
</body>


aparently "next is unexpected". iam not sure why it doesnt work. it should work. does anyone have any idea? iqm sorry if this ammount of code is a bit overkill but its just iam not sure what exactly is wrong, plz help!

ghell
07-07-2004, 05:47 PM
the best FSO reference imo is at http://www.sloppycode.net/fso/ (apart from a few bugs like calling it fso then fsobject in the next line.. but the comments usually point these out ;)

usually probles with fso are related to permissions or script blocking form things such as norton av (this looks like its just hanging so no error etc would have shown up)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum