abcdefg
asdjfka
asdfsdf
如何逐行读入这个文件的内容,并附给String str(3)
并且将处理后的str再逐行写入B.txt中,并且B.txt是随机生成的。
还有一个问题,怎么用vb来实现随机取m个整数中n个整数的方法。
以上两个问题,恳请大家能附一下关键的代码,用到的api,谢谢了!!
6 个解决方案
#1
Option Explicit
Private Sub Command1_Click()
Dim FSO As FileSystemObject
Dim oSrc As TextStream
Dim oDst As TextStream
Dim sBuf As String
Set FSO = New FileSystemObject
Set oSrc = FSO.OpenTextFile("C:\A.txt")
Set oDst = FSO.CreateTextFile("C:\B.txt")
Do
If oSrc.AtEndOfStream = True Then Exit Do
sBuf = oSrc.ReadLine
' 添加处理
oDst.WriteLine sBuf
Loop
Set oDst = Nothing
Set oSrc = Nothing
Set FSO = Nothing
End Sub
Private Sub Command1_Click()
Dim FSO As FileSystemObject
Dim oSrc As TextStream
Dim oDst As TextStream
Dim sBuf As String
Set FSO = New FileSystemObject
Set oSrc = FSO.OpenTextFile("C:\A.txt")
Set oDst = FSO.CreateTextFile("C:\B.txt")
Do
If oSrc.AtEndOfStream = True Then Exit Do
sBuf = oSrc.ReadLine
' 添加处理
oDst.WriteLine sBuf
Loop
Set oDst = Nothing
Set oSrc = Nothing
Set FSO = Nothing
End Sub
#2
Private Sub Command1_Click()
Dim str() As String
Dim i As Integer
Open "D:\a.txt" For Input As #1
Do While EOF(1) = False
ReDim Preserve str(i) As String
Line Input #1, str(i)
i = i + 1
Loop
Close #1
End Sub
Dim str() As String
Dim i As Integer
Open "D:\a.txt" For Input As #1
Do While EOF(1) = False
ReDim Preserve str(i) As String
Line Input #1, str(i)
i = i + 1
Loop
Close #1
End Sub
#3
知道一点
#4
Option Explicit
Private Sub Command1_Click()
Dim colNumber As Collection
Dim colResult As Collection
Dim i As Long
Set colNumber = New Collection
Set colResult = New Collection
For i = 0 To 99
colNumber.Add i
Next
GetSomthing colNumber, colResult, 10
Set colResult = Nothing
Set colNumber = Nothing
End Sub
Private Sub GetSomthing(ByRef colNumber As Collection, ByRef colResult As Collection, ByVal n As Long)
Dim m As Long
Dim i As Long
Dim j As Long
Dim colTemp As Collection
Set colTemp = New Collection
For i = 1 To colNumber.Count
colTemp.Add colNumber.Item(i)
Next
Randomize
For i = 1 To n
m = colTemp.Count
j = Fix(Rnd * m) + 1
colResult.Add colTemp.Item(j)
colTemp.Remove j
Next
Set colTemp = Nothing
End Sub
Private Sub Command1_Click()
Dim colNumber As Collection
Dim colResult As Collection
Dim i As Long
Set colNumber = New Collection
Set colResult = New Collection
For i = 0 To 99
colNumber.Add i
Next
GetSomthing colNumber, colResult, 10
Set colResult = Nothing
Set colNumber = Nothing
End Sub
Private Sub GetSomthing(ByRef colNumber As Collection, ByRef colResult As Collection, ByVal n As Long)
Dim m As Long
Dim i As Long
Dim j As Long
Dim colTemp As Collection
Set colTemp = New Collection
For i = 1 To colNumber.Count
colTemp.Add colNumber.Item(i)
Next
Randomize
For i = 1 To n
m = colTemp.Count
j = Fix(Rnd * m) + 1
colResult.Add colTemp.Item(j)
colTemp.Remove j
Next
Set colTemp = Nothing
End Sub
#5
Dim myreader As IO.StreamReader = New IO.StreamReader(filename, System.Text.Encoding.Default)
Dim count As Integer
Dim rws As String
Dim chararray() As Char = {", "}
rws = myreader.ReadLine()
...................
Dim count As Integer
Dim rws As String
Dim chararray() As Char = {", "}
rws = myreader.ReadLine()
...................
#6
http://community.csdn.net/Expert/topic/4825/4825949.xml?temp=.6193506
#1
Option Explicit
Private Sub Command1_Click()
Dim FSO As FileSystemObject
Dim oSrc As TextStream
Dim oDst As TextStream
Dim sBuf As String
Set FSO = New FileSystemObject
Set oSrc = FSO.OpenTextFile("C:\A.txt")
Set oDst = FSO.CreateTextFile("C:\B.txt")
Do
If oSrc.AtEndOfStream = True Then Exit Do
sBuf = oSrc.ReadLine
' 添加处理
oDst.WriteLine sBuf
Loop
Set oDst = Nothing
Set oSrc = Nothing
Set FSO = Nothing
End Sub
Private Sub Command1_Click()
Dim FSO As FileSystemObject
Dim oSrc As TextStream
Dim oDst As TextStream
Dim sBuf As String
Set FSO = New FileSystemObject
Set oSrc = FSO.OpenTextFile("C:\A.txt")
Set oDst = FSO.CreateTextFile("C:\B.txt")
Do
If oSrc.AtEndOfStream = True Then Exit Do
sBuf = oSrc.ReadLine
' 添加处理
oDst.WriteLine sBuf
Loop
Set oDst = Nothing
Set oSrc = Nothing
Set FSO = Nothing
End Sub
#2
Private Sub Command1_Click()
Dim str() As String
Dim i As Integer
Open "D:\a.txt" For Input As #1
Do While EOF(1) = False
ReDim Preserve str(i) As String
Line Input #1, str(i)
i = i + 1
Loop
Close #1
End Sub
Dim str() As String
Dim i As Integer
Open "D:\a.txt" For Input As #1
Do While EOF(1) = False
ReDim Preserve str(i) As String
Line Input #1, str(i)
i = i + 1
Loop
Close #1
End Sub
#3
知道一点
#4
Option Explicit
Private Sub Command1_Click()
Dim colNumber As Collection
Dim colResult As Collection
Dim i As Long
Set colNumber = New Collection
Set colResult = New Collection
For i = 0 To 99
colNumber.Add i
Next
GetSomthing colNumber, colResult, 10
Set colResult = Nothing
Set colNumber = Nothing
End Sub
Private Sub GetSomthing(ByRef colNumber As Collection, ByRef colResult As Collection, ByVal n As Long)
Dim m As Long
Dim i As Long
Dim j As Long
Dim colTemp As Collection
Set colTemp = New Collection
For i = 1 To colNumber.Count
colTemp.Add colNumber.Item(i)
Next
Randomize
For i = 1 To n
m = colTemp.Count
j = Fix(Rnd * m) + 1
colResult.Add colTemp.Item(j)
colTemp.Remove j
Next
Set colTemp = Nothing
End Sub
Private Sub Command1_Click()
Dim colNumber As Collection
Dim colResult As Collection
Dim i As Long
Set colNumber = New Collection
Set colResult = New Collection
For i = 0 To 99
colNumber.Add i
Next
GetSomthing colNumber, colResult, 10
Set colResult = Nothing
Set colNumber = Nothing
End Sub
Private Sub GetSomthing(ByRef colNumber As Collection, ByRef colResult As Collection, ByVal n As Long)
Dim m As Long
Dim i As Long
Dim j As Long
Dim colTemp As Collection
Set colTemp = New Collection
For i = 1 To colNumber.Count
colTemp.Add colNumber.Item(i)
Next
Randomize
For i = 1 To n
m = colTemp.Count
j = Fix(Rnd * m) + 1
colResult.Add colTemp.Item(j)
colTemp.Remove j
Next
Set colTemp = Nothing
End Sub
#5
Dim myreader As IO.StreamReader = New IO.StreamReader(filename, System.Text.Encoding.Default)
Dim count As Integer
Dim rws As String
Dim chararray() As Char = {", "}
rws = myreader.ReadLine()
...................
Dim count As Integer
Dim rws As String
Dim chararray() As Char = {", "}
rws = myreader.ReadLine()
...................
#6
http://community.csdn.net/Expert/topic/4825/4825949.xml?temp=.6193506