Last active
November 26, 2016 06:15
-
-
Save otaks/2413e53dc58813704e815afa4a9c34f8 to your computer and use it in GitHub Desktop.
改行コードLF、CRLF対応 ファイル読み込み
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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