by 三毛CAT » 2008年8月05日(火) 14:58
テキストに対して一行(¥r¥nから次の¥r¥nに囲まれた範囲)ずつ処理します。
[ここをクリックすると内容が表示されます] [ここをクリックすると非表示にします]コード: 全て選択
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
テキストに対して一行(¥r¥nから次の¥r¥nに囲まれた範囲)ずつ処理します。
[hide][code]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'行ごとの処理終了[/code][/hide]
[hide=サンプルプログラム]
テキストファイルを読み込んで一行ずつ違う色で表示します。
[code]#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[/code][/hide]