注册 登录
编程论坛 VB6论坛

vb中怎么读取txt的一列

chen123free 发布于 2015-06-14 14:28, 681 次点击
读取一个已经存在的txt中内容,输出其中大写字母P在txt中所处的行数和列数
下面是我的思路,求大神指点
Dim temp as String
Open "\test.text" For Input As #1
Open "\output.text" For Output As #3
For i = 1 To 9 '行数   
    Line Input #1, temp  '读取1行
    For j = 1 To 9  '列数
    ...
    中间少了几行不知道怎么写
    。
    If arr(i, j) = "P" Then
    Print #3, i & "," & j
    End If
next j
next i
Close #1
6 回复
#2
风吹过b2015-06-14 16:46
要把你的数据贴出来才能指点。
方法是根据数据写的。

一般可以使用 split 分解,使用 instr 查找。根据你的数据决定方法。
行号一般do循环和使用计数,而不是for 定数循环。
#3
chen123free2015-06-14 16:50
回复 2楼 风吹过b
数据大致是这样的

..P.PPP....
.P.P..PP...
..P.PPPP..
..PPPP.PP..
...P...P...
.P..PPP....

输出为所有P的坐标,(1,3),(1,5),(1,6),(1,7),(2,2),...
#4
chen123free2015-06-14 16:53
回复 2楼 风吹过b
Open App.Path + "\3.txt" For Input As #1
While Not EOF(1)
Line Input #1, arr(i, j)
For i = 1 To 10  
For j = 1 To 10
If arr(i, j) = "P" Then
Print #3, arr(i, j)  
End If
Next j
Next i
Wend
         
Close #1
Close #3
#5
中南红叶2015-06-14 18:03
我觉得读取列,用Mid(s,a,b),b = InStrRev(sFile, "\")好些
#6
风吹过b2015-06-14 21:34
程序代码:
Dim temp As String
Dim i As Long, j As Long
Open "d:\test.txt" For Input As #1                '文件名我修改了,一般是 txt 的结缀,没有 text 后缀
Open "d:\output.txt" For Output As #3
i = 0                               '行号,初始为 0
Do While Not EOF(1)                 '文件未结束,则继续循环
    Line Input #1, temp             '读取1行
    i = i + 1                       '行号
    For j = 1 To Len(temp)          '测试一行内所有数据
        If Mid(temp, j, 1) = "P" Then   '字符=P
            Print #3, i; ","; j     '输出坐标
        End If
    Next j
Loop
Close #1
Close #3


结果为:
1 , 3
 1 , 5
 1 , 6
 1 , 7
 2 , 2
 2 , 4
 2 , 7
 2 , 8
 3 , 3
 3 , 5
 3 , 6
 3 , 7
 3 , 8
 4 , 3
 4 , 4
 4 , 5
 4 , 6
 4 , 8
 4 , 9
 5 , 4
 5 , 8
 6 , 2
 6 , 5
 6 , 6
 6 , 7
#7
chen123free2015-06-15 08:35
回复 6楼 风吹过b
万分感谢
1