ASP Readline non-standard Line Endings

不问归期 提交于 2019-12-12 15:17:51

问题


I'm using the ASP Classic ReadLine() function of the File System Object.
All has been working great until someone made their import file on a Mac in TextEdit.

The line endings aren't the same, and ReadLine() reads in the entire file, not just 1 line at a time.

Is there a standard way of handling this? Some sort of page directive, or setting on the File System Object?

I guess that I could read in the entire file, and split on vbLF, then for each item, replace vbCR with "", then process the lines, one at a time, but that seems a bit kludgy.

I have searched all over for a solution to this issue, but the solutions are all along the lines of "don't save the file with Mac[sic] line endings."

Anyone have a better way of dealing with this problem?


回答1:


There is no way to change the behaviour of ReadLine, it will only recognize CRLF as a line terminator. Hence the only simply solution is the one you have already described.

Edit

Actually there is another library that ought to be available out of the box on an ASP server that might offer some help. That is the ADODB library.

The ADODB.Stream object has a LineSeparator property that can be assigned 10 or 13 to override the default CRLF it would normally use. The documentation is patchy because it doesn't describe how this can be used with ReadText. You can get the ReadText method to return the next line from the stream by passing -2 as its parameter.

Take a look at this example:-

Dim sLine
Dim oStreamIn : Set oStreamIn = CreateObject("ADODB.Stream")

oStreamIn.Type = 2 '' # Text
oStreamIn.Open 
oStreamIn.CharSet = "Windows-1252"
oStreamIn.LoadFromFile "C:\temp\test.txt"
oStreamIn.LineSeparator = 10 '' # Linefeed

Do Until oStreamIn.EOS
  sLine = oStreamIn.ReadText(-2)
  '' # Do stuff with sLine
Loop

oStreamIn.Close

Note that by default the CharSet is unicode so you will need to assign the correct CharSet being used by the file if its not Unicode. I use the word "Unicode" in the sense that the documentation does which actually means UTF-16. One advantage here is that ADODB Stream can handle UTF-8 unlike the Scripting library.

BTW, I thought MACs used a CR for line endings? Its Unix file format that uses LFs isn't it?



来源:https://stackoverflow.com/questions/1863400/asp-readline-non-standard-line-endings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!