求助!如何读取文本文件中,指定位置起,指定长度的字符串,高手请进....

求助!如何读取文本文件中,指定位置起,指定长度的字符串,高手请进。。。。

比如:有个文件:prod.txt
它包含以下内容:
001 苹果(2L) 12.00 4
002 李子(2L) 12.00 4
003 糖(2L) 12.00 4
004 苹果汁2L装 12.00 4

依次为编号(10字节)、名称(18字节)、单价(12字节)、数量(5字节)
它的格式和长度是固定的

比如说:我现在要取出所有的名称和数量到数组中去,该如何用vb代码实现阿?
我最主要的困难在于:如何定位双字节和单字节字符的问题?

清大虾帮忙阿!
谢谢了!

[368 byte] By [jamex-1+1=爱] at [2008-2-12]
# 1
do until eof(1)
lineinput #1,aline
id(i)=midb$(aline,0,10)
name(i)=midb$(aline,10,18)
price(i)=midb$(aline,28,12)
num(i)=midb$(aline,40,5)
i=i+1
loop
BitBlt-RasterOperater at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 2
mid是按字符计算,midb是按字节。
所有的字符串函数都是,带b的表示按字节。

怎么说你也是只猩猩,不会连这个都不知道把?
BitBlt-RasterOperater at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 3
能不能用filelen,你看看msdn函数吧。
tangxiaosan001-阿三 at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 4
MidB()

用InputB()直接读入即可。因为既然是MidB,那vbCr和vbLf也应该算进去
pigpag-Pigpag-AGREFighter at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 5
UDT数据结构:

Private Type record
id As String * 10
name As String * 18
price As String * 12
num As String * 6
End Type
Private Sub Command1_Click()
Dim temp, a(1 To 4) As record
Open "d:\prod.txt" For Binary As #1
For i = 1 To 4
Get #1, , a(i)
Debug.Print a(i).id
Debug.Print a(i).name
Debug.Print a(i).price
Debug.Print a(i).num
Next
Close #1
End Sub
northwolves-狼行天下 at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 6
Option Explicit
Private Type record
id As String * 10
name As String * 18
price As String * 12
num As String * 6
End Type
Private Sub Command1_Click()
Dim i As Long, a(3) As record
Open "d:\prod.txt" For Binary As #1
For i = 0 To 3
Get #1, , a(i)
Debug.Print a(i).id
Debug.Print a(i).name
Debug.Print a(i).price
Debug.Print a(i).num
Next
Close #1
End Sub
northwolves-狼行天下 at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 7
SelStart,SelLength,SelText
如果用TextBox的话
BlueBeer-1win at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 8
?这是你的问题?
jlctt-金 at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 9
Option Explicit

Private Type RecData
Num As Long
Name As String
Price As Currency
Qty As Long
End Type

Private Sub Command1_Click()
Dim i As Long, j As Long
Dim MyData() As RecData
Dim strAry() As String, strAryTmp() As String, strTmp As String

Open "C:\prod.txt" For Input As #1
strTmp = StrConv(InputB(LOF(1), #1), vbUnicode)
Close #1
strAry = Split(strTmp, vbCrLf)

ReDim MyData(UBound(strAry))
For i = 0 To UBound(strAry) - 1
strAryTmp = Split(strAry(i), " ")
MyData(i).Num = Trim(strAryTmp(0))
MyData(i).Name = Trim(strAryTmp(1))
MyData(i).Price = Trim(strAryTmp(2))
MyData(i).Qty = Trim(strAryTmp(3))

Erase strAryTmp
Next

For i = 0 To UBound(MyData) - 1
MsgBox MyData(i).Num & ", " & MyData(i).Name & ", " & MyData(i).Price & ", " & MyData(i).Qty
Next
End Sub
# 10
line input s

s=right(s,len(s)-instr(s," "))
s=trim(s)

逐个取了
flyingscv-zlj at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 11
读到一个字符串变量后全部转换为vbUnicode码,然后用Split分割得到数组
# 12
好像都不行啊!
谁是高手,再来共享一下阿!
200分跪求!
jamex-1+1=爱 at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 13
northwolves(野性的呼唤) (
同意,
azyue-沙漠之弧 at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 14
查查FILESYSTEMOBJECT看看吧
# 15
举个极端一点的文本格式的例子

0000000001苹果(2L)苹果(2L)果12.0000000004.120
0000000002李子(2L)苹果(2L)果12.0000000005.120
0000000003糖(2L)糖(2L)糖(2L)12.0000000006.120
0000000004苹果汁2L装果汁2L装12.0000000007.120
jamex-1+1=爱 at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 16
原来上这样啊,早说啊 :)

Private Sub Command1_Click()
Dim s As String

s = "0000000003糖(2L)糖(2L)糖(2L)12.0000000006.120"
MsgBox StrConv(MidB(StrConv(s, vbFromUnicode), 11, 18), vbUnicode)
End Sub
flyingscv-zlj at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 17
11 名称起始位置 字符1 汉字2
18 名称长度 字符1 汉字2
flyingscv-zlj at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 18
依次为编号(10字节)、名称(18字节)、单价(12字节)、数量(5字节)

原来有啊 ,ft
flyingscv-zlj at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 19
好像不用其它的功能就可以,帮找!
# 20
Type Record ' 定义用户自定义数据类型。
ID As Integer *10
Name As String * 18
Price as String *12
Number as String *5
End Type

Dim MyRecord As Record ' 声明变量。
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
' 若要以其他方式打开文件,必需先关闭此文件。
然后读出数据就可
.................
with myrecord
debug.print .name
debug.print .Price
debug.print .number
..................
end with
# 21
嗯,应该可以吧
kmzs-.:RNPA:.山水岿濛 at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 22
'你的问题是文件格式中存在回车符所致:

Option Explicit
Private Type record
id As String * 10
name As String * 18
price As String * 12
num As String * 5
crlf As String * 2
End Type
Private Sub Command1_Click()

Dim i As Long, a() As record, count As Long
Open "d:\prod.txt" For Binary As #1
count = LOF(1) / 47 - 1
ReDim a(count)
For i = 0 To count
Get #1, , a(i)
Debug.Print a(i).id
Debug.Print StrConv(a(i).name, vbProperCase)
Debug.Print a(i).price
Debug.Print a(i).num
Debug.Print a(i).crlf
Next
Close #1
End Sub
northwolves-狼行天下 at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 23
'd:\prod.txt 没有回车(如下)时,回车换行占了两个字节

0000000001苹果(2L)苹果(2L)果12.0000000004.1200000000002李子(2L)苹果(2L)果12.0000000005.1200000000003糖(2L)糖(2L)糖(2L)12.0000000006.1200000000004苹果汁2L装果汁2L装12.0000000007.120

Option Explicit
Private Type record
id As String * 10
name As String * 18
price As String * 12
num As String * 5
End Type
Private Sub Command1_Click()

Dim i As Long, a() As record, count As Long
Open "d:\prod.txt" For Binary As #1
count = LOF(1) / 47 - 1
ReDim a(count)
For i = 0 To count
Get #1, , a(i)
Debug.Print a(i).id
Debug.Print StrConv(a(i).name, vbProperCase)
Debug.Print a(i).price
Debug.Print a(i).num
Debug.Print
Next
Close #1
End Sub
northwolves-狼行天下 at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 24
直接这样也可以:

Option Explicit
Private Type record
id As String * 10
name As String * 18
price As String * 12
num As String * 7'--------->add a vbcrlf here
End Type
Private Sub Command1_Click()

Dim i As Long, a() As record, count As Long
Open "d:\prod.txt" For Binary As #1
count = LOF(1) / 47 - 1
ReDim a(count)
For i = 0 To count
Get #1, , a(i)
Debug.Print a(i).id
Debug.Print a(i).name
Debug.Print a(i).price
Debug.Print a(i).num
Debug.Print
Next
Close #1
End Sub
northwolves-狼行天下 at 2007-10-21 > top of Msdn China Tech,visual basic,基础类...
# 25
你在写数据的时候(*苹果**(2L)***12.00)(@李子@@(2L)@@@12.00 )