Skip to content

Instantly share code, notes, and snippets.

@otaks
Last active November 26, 2016 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otaks/2413e53dc58813704e815afa4a9c34f8 to your computer and use it in GitHub Desktop.
Save otaks/2413e53dc58813704e815afa4a9c34f8 to your computer and use it in GitHub Desktop.
改行コードLF、CRLF対応 ファイル読み込み
Option Explicit
Sub test()
Dim c As New Collection
Call readFile("D:\99.work\34\input_CRLF.txt", c)
Call readFile("D:\99.work\34\input_LF.txt", c)
Dim i As Long
For i = 1 To c.Count
Debug.Print c(i)
Next i
End Sub
'ファイル読み込み
'@param path ファイルパス
'@param file ファイル内容
Private Sub readFile(path As String, file As Collection)
If FileLen(path) = 0 Then
Exit Sub
End If
Dim buf_() As Byte
Dim buf As String
'一括取り込み
Dim FD As Long
FD = FreeFile
Open path For Binary As #FD
ReDim buf_(FileLen(path))
Get #FD, , buf_
Close #FD
buf = StrConv(buf_, vbUnicode)
If buf <> "" Then
buf = Replace(buf, vbCrLf, vbLf) '改行コードは全てLFにする
Dim rows As Variant
rows = Split(buf, vbLf)
Dim i As Long
For i = 0 To UBound(rows)
file.Add Trim(CStr(rows(i)))
Next i
End If
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment