开发者

ASP Readline non-standard Line Endings

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 Syst开发者_StackOverflow中文版em 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?


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜