| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

差分

ナビゲーションに移動 検索に移動
AppendMode = &H3&
End Enum
 
===UTF-8 に対応===
<pre>
Option Explicit
 
Private Const INVALID_FILENO As Integer = -1
 
Private stream As Object
 
Private Const adTypeText = 2
 
Private Const adModeRead = 1
Private Const admodewrite = 2
Private Const adModeReadWrite = 3
 
Private Const adStateClosed = 0
Private Const adReadLine = -2
Private Const adCR = 13
 
' 処理対象のファイル名を保持
Private m_Filename As String
 
' 処理中のファイル番号を保持
Private m_FileNo As Integer
'
' 初期化
'
Private Sub Class_Initialize()
Set stream = CreateObject("ADODB.Stream")
'Call setFileNo(INVALID_FILENO)
 
End Sub
 
'
' 終了処理
'
Private Sub Class_Terminate()
 
Call closeFile
 
Set stream = Nothing
 
End Sub
'
' ファイルが開かれているか
'
Public Function isOpen() As Boolean
' https://docs.microsoft.com/ja-jp/sql/ado/reference/ado-api/state-property-ado?view=sql-server-ver15
'isOpen = (getFileNo > 0)
isOpen = (stream.State <> adStateClosed)
 
End Function
'
' ファイルを開く
'
Public Function openFile(fileName As String, mode As FileMode) As Boolean
On Error GoTo errHandler
Dim isAppend As Boolean
isAppend = False
openFile = True
stream.Charset = CHAR_SET
stream.Type = adTypeText
'https://docs.microsoft.com/ja-jp/office/client-developer/access/desktop-database-reference/lineseparator-property-ado
stream.LineSeparator = adCR
Call setFilename(fileName)
'Call setFileNo(FreeFile)
Select Case mode
Case FileMode.InputMode
'Open getFilename For Input As getFileNo
stream.mode = adModeReadWrite
Case FileMode.OutputMode
'Open getFilename For Output As getFileNo
stream.mode = admodewrite
Case FileMode.AppendMode
'Open getFilename For Append As getFileNo
stream.mode = admodewrite
isAppend = True
Case Else
openFile = False
End Select
If openFile Then
stream.Open
stream.LoadFromFile fileName
If isAppend Then
'https://docs.microsoft.com/ja-jp/office/client-developer/access/desktop-database-reference/seteos-method-ado
stream.SetEOS = True
Else
stream.Position = 0
End If
End If
Exit Function
errHandler:
openFile = False
Debug.Print Err.Number & ":" & Err.Description
End Function
'
' ファイルから1行読込む
'
Public Function readLine(ByRef line As String) As Boolean
Dim ret As Boolean
ret = Not isEOF()
If ret Then
'Line Input #getFileNo(), line
line = stream.ReadText(adReadLine)
End If
readLine = ret
End Function
'
' ファイルがEOFに達しているか
'
Public Function isEOF() As Boolean
 
'isEOF = EOF(getFileNo())
isEOF = stream.EOS
End Function
'
' 1行出力
'
Public Sub println(str As String)
 
'Print #getFileNo(), str
stream.WriteText str
 
End Sub
'
' ファイルを閉じる
'
Public Sub closeFile()
'Close getFileNo()
If isOpen() Then
stream.Close
End If
 
End Sub
 
'
' ファイル名の設定
'
Private Sub setFilename(newVal As String)
 
m_Filename = newVal
 
End Sub
'
' ファイル名の取得
'
Public Function getFilename() As String
 
getFilename = m_Filename
 
End Function
'
' ファイルNoの設定
'
Private Sub setFileNo(newVal As Integer)
 
m_FileNo = newVal
 
End Sub
'
' ファイルNoの取得
'
Public Function getFileNo() As Integer
 
getFileNo = m_FileNo
 
End Function
</pre>
 
{{ref FileUtil.cls}}

案内メニュー