習(xí)題八
第1,2,6,7,9題答案可閱讀教材找到。
3.寫Open語(yǔ)句
(1) Open "Seqnew.dat" For Output As #1 '重寫
或 Open "Seqnew.dat" For Append As #1 '添加
(2)Open "Seqold.dat" For Input As #2
(3)Open "Seqappend.dat" For Append As FreeFile
4.編程,將文件Text.dat 讀入變量strTest。
Dim strTest As String
Open "Text.dat" For Input As #2
Do While Not EOF(2)
strTest = strTest + Input(1, #2)
Loop
8.本課程不涉及二進(jìn)制文件。故此處以順序文件讀寫方式編程。另外合并的結(jié)果可以是合并到其中某一個(gè)文件,也可以是合并到一個(gè)新的文件,此處代碼是合并到其中一個(gè)文件F1.txt。
'方法一,在2號(hào)文件一次讀一個(gè)字符并寫入1號(hào)文件
Dim strTemp As String * 1
Open "d:\F1.txt" For Append As #1
Open "d:\F2.txt" For Input As #2
Do While Not EOF(2)
strTemp = Input(1, #2)
Print #1, strTemp; '注意結(jié)尾有分號(hào),否則每個(gè)字符一行
Loop
Close
'方法二,在2號(hào)文件一次讀一個(gè)字符并累加,最后將累加結(jié)果寫入1號(hào)文件
Dim strTemp As String '注意方法一變量定義的區(qū)別
Open "d:\F1.txt" For Append As #1
Open "d:\F2.txt" For Input As #2
strTemp = ""
Do While Not EOF(2)
strTemp = strTemp + Input(1, #2)
Loop
Print #1, strTemp
Close