RELATIVE FILE(固定長ファイル)の操作
COBOL
VB.NET
固定長ファイルはデータ項目を全て文字として扱います。
CRLFコードを末尾に付加しなければなりません。
構造体の複写は出来ません。
Module Module1
Structure REC1
<VBFixedString(40)> Public A As String
<VBFixedString(11)> Public B As String
<VBFixedString(1)> Public C As String
<VBFixedString(2)> Public crlf As String
End Structure
Structure REC2
<VBFixedString(40)> Public A As String
<VBFixedString(11)> Public B As String
<VBFixedString(1)> Public C As String
<VBFixedString(19)> Public D As String
<VBFixedString(2)> Public crlf As String
End Structure
Dim count As Integer = 0
Sub Main()
W() '5件のデータを作成
N() 'レコード数を計数
R() '書き込んだデータを画面表示
U() '別のファイルに転記
End Sub
'レコードの作成
Private Sub W()
Dim FileNo As Integer = FileSystem.FreeFile
FileSystem.FileOpen(FileNo, "c:\test.txt", OpenMode.Random, , , Len(New REC1()))
Dim WR As New REC1
For Rec_No As Integer = 1 To 5
WR.A = "COBOL的VB.NET"
WR.B = Format(Rec_No, "00000000000")
WR.C = "x"
WR.crlf = ControlChars.CrLf
FileSystem.FilePut(FileNo, WR, Rec_No)
Next
FileSystem.FileClose(FileNo) 'ファイルを閉じる
End Sub
'レコードの読み込み
Private Sub R()
Dim FileNo As Integer = FileSystem.FreeFile
FileSystem.FileOpen(FileNo, "c:\test.txt", OpenMode.Random, , , Len(New REC1()))
Dim RR As New REC1
Dim Rec_No As Integer = 1
FileSystem.FileGet(FileNo, RR, Rec_No)
Do Until EOF(FileNo)
Console.WriteLine(Rec_No & ":")
Console.WriteLine(RR.A)
Console.WriteLine(RR.B)
Console.WriteLine(RR.C)
FileSystem.FileGet(FileNo, RR, Rec_No)
Rec_No += 1
Loop
FileSystem.FileClose(FileNo) 'ファイルを閉じる
End Sub
'レコードの転記
Private Sub U()
Dim FileNoR As Integer = FileSystem.FreeFile
FileSystem.FileOpen(FileNoR, "c:\test.txt", OpenMode.Random, , , Len(New REC1()))
Dim FileNoU As Integer = FileSystem.FreeFile
FileSystem.FileOpen(FileNoU, "c:\testU.txt", OpenMode.Random, , , Len(New REC2()))
Dim R As New REC1
Dim U As New REC2
Dim R_No As Integer = 1
FileSystem.FileGet(FileNoR, R, R_No)
Do Until EOF(FileNoR)
U.A = R.A
U.B = R.B
U.C = R.C
U.D = Format(Now, "yyyy/MM/dd HH:mm:ss")
U.crlf = ControlChars.CrLf
FileSystem.FilePut(FileNoU, U, R_No)
R_No += 1
FileSystem.FileGet(FileNoR, R, R_No)
Loop
FileSystem.FileClose(FileNoR) 'ファイルを閉じる
FileSystem.FileClose(FileNoU) 'ファイルを閉じる
End Sub
'レコードの総数を取得
Private Sub N()
If System.IO.File.Exists("c:\test.txt") Then
count = FileLen("c:\test.txt") / Len(New REC1())
Console.WriteLine(count.ToString)
End If
End Sub
End Module
お役に立てましたか?
IT、プログラミングのランキングサイトはこちらです。
最新記事の自動受信登録はこちらです。 |
![]() |
![]() |
![]() |
![]() |
| 固定リンク







コメント