| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1011 人关注过本帖, 1 人收藏
标题:VB读取txt文件
只看楼主 加入收藏
z18234089392
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2015-5-29
结帖率:83.33%
收藏(1)
已结贴  问题点数:20 回复次数:9 
VB读取txt文件
'读入河流参数,读入断面参数
Dim ss As String
   Dim t As Long
   Dim aa
   Dim tot As Long
Open App.Path & "\jilu.txt" For Input As #1
Do While Not EOF(1)
 Line Input #1, ss
      ss = Trim(ss)
      ss = Replace(ss, "  ", ",")
      ss = Replace(ss, ",,", ",")
      aa = Split(ss, ",")
      tot = UBound(aa)
      
      If t = 1 Then
         '第一行为河流参数
         HL = aa(0) '第1列
         Q = aa(1) '第2列
         NN = aa(2) '第3列
         Z(1) = aa(3) '第4列
         t = t + 1
      Else
         If tot = 4 Then
            I = aa(0)
            L(I) = aa(1)
            M(I) = aa(2)
            GG(I) = aa(3)
            n(I) = aa(4)
            t = t + 1
         Else
            AX(I, M(I)) = aa(0)
            GCH(I, M(I)) = aa(1)
         End If
      End If
Loop
Close #1
我的除了AX 和GCH是二维 其它都是一维的,运行后这两行显示类型不匹配,应该怎么改
2015-05-29 12:20
lianyicq
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:26
帖 子:737
专家分:3488
注 册:2013-1-26
收藏
得分:0 
两个二维数组的下标用到了从文本文件中取的值。
在AX(I, M(I)) = aa(0)处设断点,看看I和M(I)到底是什么。
再注意数据类型尽量匹配。
要是Line Input #1, ss
执行后ss中存在连续3个或3个以上空格怎么办呢。

大开眼界
2015-05-29 14:19
z18234089392
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2015-5-29
收藏
得分:0 
不是太明白,我发现这个读取数据不是自己想象的那么简单。。
能不能直接跳过空格读取数据呢。我发现我这样编的话很多都读的是空的。
而且aa(0)好像被前面赋值了,后面的出现了的aa(0)还是那个值
方便的话能加下QQ么 我已经被这个问题困扰三天进行不下去了。。。。
我的QQ:837051349  谢谢
2015-05-29 15:49
lianyicq
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:26
帖 子:737
专家分:3488
注 册:2013-1-26
收藏
得分:5 
回复 3楼 z18234089392
给你一个间隔多个空格替换为一个空格的函数
程序代码:
Function FormatStr(temp As String) As String
  FormatStr= Trim(temp)
  Do While InStr(1, FormatStr, "  ") > 0 
     FormatStr = Replace(FormatStr, "  ", " ")
   Loop
End Function
可以把jilu.txt部分传来试试

大开眼界
2015-05-29 16:16
z18234089392
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2015-5-29
收藏
得分:0 
ALGOU          320           2             90
 1             0             11            0             .04
 0             96
 0             88.7
 4             86.1
 12            85.9
 17.2          85.6
 19.5          86.1
 20            86.4
 27.2          88.5
 32.6          88.8
 42.3          88.5
 42.3          96
 2             39            12            0             .04
 0             97
 0             90.2
 3.4           88
 7.2           86
 12.5          86.3
 21.6          86.7
 27.4          87.2
 33            90.1
 40            90.1
 40.6          89.7
 46.4          87.3
 46.4          97
这是数据部分,是这样的,我的数据有很多组这只是其中的两组,第一行是河流参数,第二行是第一个断面的参数,3-12行是第一个断面的地形点参数,13行是第二个断面的参数,14-25行是第二个断面的地形点参数。
程序运行时不只是两个断面,可能会有很多个,而且每个断面的地形点个数也不一样。
后面的计算要用到这些参数,所以要读取进去。我现在总觉得我用aa(0),aa(1)....后面的读取的值会覆盖了前面读取的数。谢谢了。实在是头大啊
2015-05-29 18:24
lianyicq
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:26
帖 子:737
专家分:3488
注 册:2013-1-26
收藏
得分:10 
回复 5楼 z18234089392
可以参照https://bbs.bccn.net/thread-445824-1-1.html 7楼看看如何按地址引用结构体参数。不想按地址调用,就把下面代码看明白了,适当调整。
程序代码:
Private Type PP 'Point with 2 parameters
  x As Single
  y As Single
End Type

Private Type SP 'section
  a As Single
  b As Single
  c As Single
  d As Single
  e As Single
  pointp() As PP
End Type

Private Type RP
  a As String
  b As Single
  c As Single
  d As Single
End Type

Dim sectionP() As SP
Dim river As RP


Private Sub Form_Load()
  Dim TempLine As String
  Dim temp() As String
  Dim SectionCount As Integer
  Dim PointCount As Integer
  Dim SAdd As Boolean

 
  Open "c:\test.txt" For Input As #1
  Do While Not EOF(1)
    Line Input #1, TempLine
      TempLine = FormatStr(TempLine)
      temp = Split(TempLine, " ")
      Select Case UBound(temp)
        Case Is = 3
          river.a = temp(0)
          river.b = Val(temp(1))
          river.c = Val(temp(2))
          river.d = Val(temp(3))
        Case Is = 4
          SAdd = Not (SAdd)
          If SAdd = False Then SectionCount = SectionCount + 1
          ReDim Preserve sectionP(SectionCount)
          PointCount = 0
          sectionP(SectionCount).a = Val(temp(0))
          sectionP(SectionCount).b = Val(temp(1))
          sectionP(SectionCount).c = Val(temp(2))
          sectionP(SectionCount).d = Val(temp(3))
          sectionP(SectionCount).e = Val(temp(4))
          
       Case Is = 1
          ReDim Preserve sectionP(SectionCount).pointp(PointCount)
          sectionP(SectionCount).pointp(PointCount).x = Val(temp(0))
          sectionP(SectionCount).pointp(PointCount).y = Val(temp(1))
          PointCount = PointCount + 1
       End Select
    Loop
    Close #1
    ShowDatum
End Sub

Function FormatStr(temp As String) As String
  FormatStr = Trim(temp)
  Do While InStr(1, FormatStr, "  ") > 0
     FormatStr = Replace(FormatStr, "  ", " ")
   Loop
End Function
Sub ShowDatum()
  Dim i As Integer
  Dim j As Integer
  For i = 0 To UBound(sectionP)
    Text1.Text = Text1.Text & sectionP(i).a & " " & sectionP(i).b & " " & sectionP(i).c & " " & sectionP(i).d & " " & sectionP(i).e & vbCrLf
    For j = 0 To UBound(sectionP(i).pointp)
      Text1.Text = Text1.Text & sectionP(i).pointp(j).x & " " & sectionP(i).pointp(j).y & vbCrLf
    Next
  Next

 
End Sub



大开眼界
2015-05-29 20:08
z18234089392
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2015-5-29
收藏
得分:0 
dim HL As String
dim Q As Double '流量Q
dim NN As Integer '断面个数NN
dim L(100) As Double '距前一段面的距离L
dim GG(100) As Double '局部阻力系数GG
dim n(100) As Double '糙率N
dim M(100) As Integer '各断面地形点总个数
dim AX(100, 50) As Double '各断面地形点距左岸第一点水平距离
dim GCH(100, 50) As Double '各断面地形点高程
dim Z(100) As Double '水位
Dim TempLine As String
Dim temp() As String
dim I As Integer
Dim J As Integer
Dim SAdd As Boolean
Open App.Path & "\jilu.txt" For Input As #1
  Do While Not EOF(1)
    Line Input #1, TempLine
      TempLine = FormatStr(TempLine)
      temp = Split(TempLine, " ")
      Select Case UBound(temp)
        Case Is = 3
          HL = temp(0)
          Q = Val(temp(1))
          NN = Val(temp(2))
          Z(1) = Val(temp(3))
        Case Is = 4
          SAdd = Not (SAdd)
          If SAdd = False Then I = I + 1
           J = 0
           I = Val(temp(0))
           L(I) = Val(temp(1))
           M(I) = Val(temp(2))
           GG(I) = Val(temp(3))
           n(I) = Val(temp(4))
       Case Is = 1
          AX(I, J) = Val(temp(0))
          GCH(I, J) = Val(temp(1))
          J = J + 1
       End Select
    Loop
    Close

这样能运行,可是还是不太对,这是怎么回事啊
AX(I, J) = Val(temp(0))
GCH(I, J) = Val(temp(1))

我想弄成这种形式的。。。。
2015-05-29 22:29
lianyicq
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:26
帖 子:737
专家分:3488
注 册:2013-1-26
收藏
得分:5 
回复 7楼 z18234089392
代码没问题,二维数组中第n列是第n个断面地形点参数.
关闭文件后,输出到文本框显示是这样,
0 96 97 0 0 0 0 0 0 0 0
0 88.7 90.2 0 0 0 0 0 0 0 0
0 86.1 88 0 0 0 0 0 0 0 0
0 85.9 86 0 0 0 0 0 0 0 0
0 85.6 86.3 0 0 0 0 0 0 0 0
0 86.1 86.7 0 0 0 0 0 0 0 0
0 86.4 87.2 0 0 0 0 0 0 0 0
0 88.5 90.1 0 0 0 0 0 0 0 0
0 88.8 90.1 0 0 0 0 0 0 0 0
0 88.5 89.7 0 0 0 0 0 0 0 0
0 96 87.3 0 0 0 0 0 0 0 0
0 0 97 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
输出是I=0 to 10 表示各断面号,J=0 to 30 表示第I个断面对第J个GCH值.
如果按我给你的代码以结构体存储要方便一些.
 

大开眼界
2015-06-01 09:07
csl591
Rank: 4
等 级:贵宾
威 望:10
帖 子:78
专家分:92
注 册:2009-2-5
收藏
得分:0 
print与write有什么区别?
2015-06-09 08:50
lianyicq
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:26
帖 子:737
专家分:3488
注 册:2013-1-26
收藏
得分:0 
回复 9楼 csl591
请另外开贴,便于大家以后查找类似问题.开贴了,我再给些建方式

大开眼界
2015-06-09 08:56
快速回复:VB读取txt文件
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021076 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved