導航:首頁 > 文件教程 > vb中怎麼讀取txt文件

vb中怎麼讀取txt文件

發布時間:2024-07-12 09:30:25

1. vb 怎樣讀取TXT文件中的數據

dim a,b,c,d as integer
dim as,bs,cs,ds as string
Open "TESTFILE" For Input As #1 \' 打開文件。
Do While Not EOF(1) \'
Input #1, as
Input #1, bs
Input #1, cs
Input #1, ds
Loop
Close #1 \' 關閉文件
a=val(mid$(as,3,len(as)-3))
b=val(mid$(bs,3,len(as)-3))
c=val(mid$(cs,3,len(as)-3))
d=val(mid$(ds,3,len(as)-3))
'提取數據給變數

2. vb讀取文件夾中所有的txt文件

使用VB內建函數讀取文本文件

1/3

雙擊Command1添加如下代碼

Private Sub Command1_Click()

    Dim strFile     As String

    Dim intFile     As Integer

    Dim strData     As String

    strFile = "c:\學生成績.txt"

    intFile = FreeFile

    Open strFile For Input As intFile

    strData = StrConv(InputB(FileLen(strFile), intFile), vbUnicode)

    Debug.Print strData

    Close intFile

End Sub

2/3

運行代碼讀取文件

按F8開始單步調試代碼,點擊Command1,進入單步調試功能,多次按下F8或直接按下F5運行完成,就完成了讀取文本文件內容並輸出到立即窗口。

查看剩餘1張圖

3/3

關鍵代碼說明

intFile = FreeFile:獲取一個文件句柄

Open strFile For Input As intFile :打開文件

FileLen(strFile) :獲取文件內容位元組大小

InputB:讀取文件內容位元組流

StrConv:將位元組流轉換為Unicode字元串

Debug.Print strData:將字元串內容輸出到立即窗口

Close intFile:關閉文件句柄

使用FileSystemObject讀取文本文件

1/3

添加Microsoft Scripting Runtime引用

點擊Project菜單,再點擊下拉菜單中的Reference,打開引用對話框,瀏覽找到Microsoft Scripting Runtime引用,選擇後點確定按鈕。

查看剩餘1張圖

2/3

雙擊Command2添加如下代碼

Private Sub Command2_Click()

    Dim objFS

3/3

運行代碼讀取文件

按F8開始單步調試代碼,點擊Command2,進入單步調試功能,多次按下F8或直接按下F5運行完成,就完成了讀取文本文件內容並輸出到立即窗口。

3. 怎麼使用VB打開文本文件

這樣你換一種寫法試一試:
dim x As String
open "g:\雜項\123.txt" for input as #1
DO while not eof(1)
line input #1,x
text1.text = text1.text & x & vbcrlf
loop
close #1
還有一種是:版
open "g:\雜項\123.txt" for input as #1
text1.text = strconv(inputb(lof(2),#1),vbunicode)
close #1
不行的話 再問權我

4. VB如何讀取txt數據

Private
Sub
Command1_Click()
'基本讀取方法---只能讀取非中文
'
Dim
a
As
String,
b
As
String,
c
As
String,
d
As
String,
e
As
String,
f
As
String'定義幾個變數
Dim
FileNo
As
Integer
FileNo
=
FreeFile()
'獲取一個未使用的文件號
Dim
str
As
String
'用來記錄最終的值
Open
"你要讀取文件的完整路徑"
For
Input
As
#FileNo
While
Not
EOF(FileNo)
Dim
str_Read
As
String
Input
#FileNo,
str_Read
'讀取一個字元到變數str_Read---不包含換行回車符,也不包括逗號,分號及Tab,當讀到分隔符號(前面列舉的4種)時就賦值給後面的變數,如果有多個變數就讀取相對應多的分隔數據
'如果為你的007.txt文件且為你列舉的內容那麼上面一句就改成下面的語句
'
Input
#FileNo,
a,
b,
c,
d,
e,
f
'
str
=
str
&
str_Read
Wend
'Me.Text1.Text
=
str
Close
#FileNo
End
Sub

5. 請問VB中如何讀取txt文件的數據

1、新建一個標準的VB EXE工程,只有一個Form,Form上有兩個按鈕:Command1和Command2。

閱讀全文

與vb中怎麼讀取txt文件相關的資料

熱點內容
電腦隱藏u盤文件病毒 瀏覽:31
dubbojava啟動 瀏覽:921
調查問卷數據怎麼處理 瀏覽:442
java調用vb 瀏覽:85
金庸群俠傳存檔文件 瀏覽:293
starknight安卓版 瀏覽:129
雙減政策原文件在哪裡看 瀏覽:691
win7怎麼設置列印機網路共享 瀏覽:279
小說類型的網站怎麼優化 瀏覽:897
視頻網站怎麼學習 瀏覽:666
蘋果2017年2代se參數 瀏覽:428
藍線是哪個app上的 瀏覽:271
打開數據為什麼網路沒法用 瀏覽:314
台式電腦連接無線網路怎麼連接網路連接 瀏覽:300
網路傳輸介質的選擇 瀏覽:253
fpga設計教程pdf 瀏覽:352
在哪個網站學資料庫 瀏覽:705
jsp中包含外部文件的方式 瀏覽:179
applewatch打電話功能 瀏覽:73
編程中無法載入圖片是怎麼回事 瀏覽:487

友情鏈接