用VB怎样修改txt文件中指定行和指定列的数据,请各位高手赐教!!! 例如: txt文件数据如下: 25
例如:txt文件数据如下:v132,25,26,3365,63,65,3125,21,23,3221,25,24,42如果要将上面数据文件的第三列的数据“26”65232...
例如: txt文件数据如下:
v1
32,25,26,33
65,63,65,31
25,21,23,32
21,25,24,42
如果要将上面数据文件的第三列的数据“26 ”
65
23
24
换成另外从窗口输入的一组数据,怎么用程序实现啊?求源代码?灰常感谢!
例如:也就是说如何用程序实现将上面数据文件的第三列数据“26 65 23 24 ”换成10,20,30,25 展开
v1
32,25,26,33
65,63,65,31
25,21,23,32
21,25,24,42
如果要将上面数据文件的第三列的数据“26 ”
65
23
24
换成另外从窗口输入的一组数据,怎么用程序实现啊?求源代码?灰常感谢!
例如:也就是说如何用程序实现将上面数据文件的第三列数据“26 65 23 24 ”换成10,20,30,25 展开
2个回答
展开全部
Private Sub Command1_Click()
Dim ss() As String, s As String
dim i as integer
dim tt() as string
Open "d:\123.txt" For Input As #1
s = StrConv(InputB(LOF(1), #1), vbUnicode)
Close #1
ss = Split(s, vbCrLf)
ss(0)=replace(ss(0),",26,",",10,")
ss(1)=replace(ss(0),",65,",",20,")
ss(2)=replace(ss(0),",23,",",30,")
ss(3)=replace(ss(0),",24,",",25,")
open "d:\123.txt" For output As #1
for i=0 to ubound(ss)
print #1,ss(i)
next i
close
end sub
'大体意思就是这样,手写的代码,楼主自己测试
Dim ss() As String, s As String
dim i as integer
dim tt() as string
Open "d:\123.txt" For Input As #1
s = StrConv(InputB(LOF(1), #1), vbUnicode)
Close #1
ss = Split(s, vbCrLf)
ss(0)=replace(ss(0),",26,",",10,")
ss(1)=replace(ss(0),",65,",",20,")
ss(2)=replace(ss(0),",23,",",30,")
ss(3)=replace(ss(0),",24,",",25,")
open "d:\123.txt" For output As #1
for i=0 to ubound(ss)
print #1,ss(i)
next i
close
end sub
'大体意思就是这样,手写的代码,楼主自己测试
追问
非常感谢您的回答,您的程序确实可以先查找某一行的某个数再替换,但是现在遇到一个问题,如果某一行中出现几个相同的数,就都被替换了,还是不能实现对指定行和指定列修改。请问如果用二维数组,怎样实现:首先将这个矩阵的数一一赋值到二维数组中去,然后再修改二维数组元素的值,最后保存在txt文件中?求源代码,谢谢!
追答
用以下函数替换:
调用方法,rStr 源字符串;c 要替换的列;tStr 换成后列的内容
如:ss(0)=myReplace(ss(0),3,10)
意思就是把ss(0)第3列的数据替换成 10;
Private Function myReplace(rStr As String, c As Integer, tStr As String) As String
Dim k As Integer, i As Integer, k2 As Integer
For i = 1 To c - 1
k = InStr(k + 1, rStr, ",")
Next i
k2 = InStr(k + 1, rStr, ",")
myReplace = Left(rStr, k) & tStr & Right(rStr, Len(rStr) - k2 + 1)
End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询