[ここをクリックすると内容が表示されます]
コード: 全て選択
Dim pos_s As DWord
Dim pos_e As DWord
Dim buffer As String
Dim line As String
Dim lineno As DWord
buffer="" 'bufferに処理する文字列を入れる
pos_s=1'行ごとの処理開始
lineno=1'行番号
pos_e=InStr(pos_s,buffer,Ex"\r\n")
if pos_e=0 Then pos_e=Len(buffer)
While pos_s<Len(buffer)
if pos_s<>pos_e Then line=Mid$(buffer,pos_s,pos_e-pos_s) Else line=""
'一行の文字列はlineに代入される
'ここに処理を書く
pos_s=pos_e+2
pos_e=InStr(pos_s,buffer,Ex"\r\n")
if pos_e=0 Then pos_e=Len(buffer)
lineno++
Wend'行ごとの処理終了
サンプルプログラム [ここをクリックすると内容が表示されます]
テキストファイルを読み込んで一行ずつ違う色で表示します。
コード: 全て選択
#N88BASIC
Dim pos_s As DWord
Dim pos_e As DWord
Dim buffer As String
Dim line As String
Dim lineno As DWord
Dim name As String
Dim hFile As HANDLE
Dim length As DWord
Dim alength As DWord
*start
Input "ファイル名:",name
hFile=CreateFile(name,GENERIC_READ,0,ByVal 0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0)
if hFile=INVALID_HANDLE_VALUE Then
color 2
Print "ファイルのオープンに失敗しました。"
color 7
Goto *start
End If
length=GetFileSize(hFile,0)
buffer=ZeroString(length)
ReadFile(hFile,StrPtr(buffer),length,VarPtr(alength),ByVal 0)
CloseHandle(hFile)
pos_s=1'行ごとの処理開始
lineno=1
pos_e=InStr(pos_s,buffer,Ex"\r\n")
if pos_e=0 Then pos_e=Len(buffer)
While pos_s<Len(buffer)
if pos_s<>pos_e Then line=Mid$(buffer,pos_s,pos_e-pos_s) Else line=""
color lineno mod 7+1
Print lineno;"行目:";line
pos_s=pos_e+2
pos_e=InStr(pos_s,buffer,Ex"\r\n")
if pos_e=0 Then pos_e=Len(buffer)
lineno++
Wend'行ごとの処理終了
color 7
goto *start