网络编程
位置:首页>> 网络编程>> Asp编程>> asp解决fso.ReadAll提示输入超出了文件尾的错误

asp解决fso.ReadAll提示输入超出了文件尾的错误

 来源:asp之家 发布时间:2008-01-30 21:40:00 

标签:fso,ReadAll,文件

最近在用fso,读取txt文本文件的内容时碰到了“输入超出了文件尾 ”的运行错误,当txt中的内容为空的时候就出现这个问题了,查了下是ReadAll的用法问题,特地转来与大家分享。

解决方法一(from:9enjoy.com

Function getfile(filename)
  dim f
  if fso.fileExists(server.MapPath(filename)) then
  set f = fso.OpenTextFile(server.MapPath(filename))
  Response.write f.ReadAll
  set f = nothing
  end if
End Function

调用时出现错误:

Microsoft VBScript 运行时错误 错误 '800a003e'  

输入超出了文件尾  

就是Response.write f.ReadAll这句。

这是一段读取已经存在的文件,并输出文件内容的函数。但当这个文件没有内容时,就会出这种错误提示。

查了文档,ReadAll不会自己判断是否到文件尾,只好,加了个atendofstream的判断语句,测试OK。

更改后的代码为

Function getfile(filename)
  dim f
  if fso.fileExists(server.MapPath(filename)) then
  set f = fso.OpenTextFile(server.MapPath(filename))
  if not f.atendofstream then  
  Response.write f.ReadAll
  end if
  set f = nothing
  end if
End Function

解决方法二( form 秋浦河畔):

Function ReadTemplate(TemplateName)
    Dim objFSO,objMyFSO
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    Set objMyFSO = objFSO.OpenTextFile(Server.MapPath(TemplateName),1,True)  '读取文本文件
    ReadTemplate = objMyFSO.ReadAll&info()      'textStream对象的readAll方法,读取全部文件并返回结果字符串
    objMyFSO.Close
    Set objMyFSO = Nothing
    Set objFSO = Nothing
End Function

如果内容不存在,则会出现“输入超出了文件尾”错误,解决方法:传递真实参数即可,或者不确定的话先判断文件是否存在再执行。

例如:

set copyss=fso.OpenTextFile(path,1)
Set f = fso.GetFile(path)
intSizeB = f.Size
    if intsizeb>0 then
    copystring = copyss.ReadAll
    end if

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com