|
-
Aug 16th, 2001, 09:40 PM
#1
Thread Starter
Hyperactive Member
Partial string replacement in text file
I have a text file with data like this
-1100,1,0,0,100.00
2050,1,0,0,100.00
-23600,4,0,0,10.00
-174,1,0,0,20.00
0,-1,0,0,0
I’m having a hard time trying to figure out how to replace
a certain number on a specific line.
example:
I want to replace the first number on line three -23600
to -24000.
results:
-1100,1,0,0,100.00
2050,1,0,0,100.00
-24000,4,0,0,10.00
-174,1,0,0,20.00
0,-1,0,0,0
thank you for your help.
Jason
-
Aug 16th, 2001, 10:11 PM
#2
PowerPoster
didnt test this, but give it a try
VB Code:
Sourcefilename = "c:\test.whatever"
FF = FreeFile
Open Sourcefilename For Binary As #FF
strfile = Space$(LOF(FF))
Get #FF, 1, strfile
Close #FF
strarray1()=split(strfile,vbcrlf)
strarray2()=split(strarray1(2),",")
strarray2(0)="-24000"
strarray1(2)=join(strarray2,",")
strfile=join(strarray1,vbcrlf)
open SourceFilename for output as #FF
print #FF, strfile
close #FF
-
Aug 16th, 2001, 10:21 PM
#3
PowerPoster
or genrealize the problem like so (still not tested):
VB Code:
sub ReplaceNumber(RowNumber as Integer, ColumnNumber as Integer)
Dim strfile as string, strarray1() as string, strarray2() as string
Dim FF as Integer
Sourcefilename = "c:\test.whatever"
FF = FreeFile
Open Sourcefilename For Binary As #FF
strfile = Space$(LOF(FF))
Get #FF, 1, strfile
Close #FF
strarray1()=split(strfile,vbcrlf)
strarray2()=split(strarray1(RowNumber-1),",")
strarray2(ColumnNumber-1)="-24000"
strarray1(RowNumber-1)=join(strarray2,",")
strfile=join(strarray1,vbcrlf)
open SourceFilename for output as #FF
print #FF, strfile
close #FF
-
Aug 16th, 2001, 10:32 PM
#4
PowerPoster
actually prolly better as (still not tested):
VB Code:
sub ReplaceNumber(RowNumber as Integer, ColumnNumber as Integer, RplString as String)
Dim strfile as string, strarray1() as string, strarray2() as string
Dim FF as Integer
Sourcefilename = "c:\test.whatever"
FF = FreeFile
Open Sourcefilename For Binary As #FF
strfile = Space$(LOF(FF))
Get #FF, 1, strfile
Close #FF
strarray1()=split(strfile,vbcrlf)
strarray2()=split(strarray1(RowNumber-1),",")
strarray2(ColumnNumber-1)=ReplString
strarray1(RowNumber-1)=join(strarray2,",")
strfile=join(strarray1,vbcrlf)
open SourceFilename for output as #FF
print #FF, strfile
close #FF
End Sub
-
Aug 16th, 2001, 10:34 PM
#5
PowerPoster
or even (still not tested):
VB Code:
sub ReplaceNumber(AFilename as String, RowNumber as Integer, ColumnNumber as Integer, RplString as String)
Dim strfile as string, strarray1() as string, strarray2() as string
Dim FF as Integer
FF = FreeFile
Open Afilename For Binary As #FF
strfile = Space$(LOF(FF))
Get #FF, 1, strfile
Close #FF
strarray1()=split(strfile,vbcrlf)
strarray2()=split(strarray1(RowNumber-1),",")
strarray2(ColumnNumber-1)=ReplString
strarray1(RowNumber-1)=join(strarray2,",")
strfile=join(strarray1,vbcrlf)
open SourceFilename for output as #FF
print #FF, strfile
close #FF
End Sub
-
Aug 16th, 2001, 11:14 PM
#6
Thread Starter
Hyperactive Member
Muddy thank you for your help. Code works great. If you wouldn't mind
Could you add commits to the code so I learn what the code is doing?
Thank you very much for your help..
Jason
-
Aug 16th, 2001, 11:30 PM
#7
PowerPoster
OK, Ill make a big admission here ... Im not much of a commenter. Heres my best shot. Hope it helps!
btw are you sure this worked? I just found an error corrected below:
VB Code:
sub ReplaceNumber(AFilename as String, RowNumber as Integer, ColumnNumber as Integer, RplString as String)
Dim strfile as string, strarray1() as string, strarray2() as string
Dim FF as Integer
FF = FreeFile
Open Afilename For Binary As #FF
strfile = Space$(LOF(FF))
Get #FF, 1, strfile 'read the entire cotents of the file into a variable
Close #FF
strarray1()=split(strfile,vbcrlf) 'split the file up by rows into strarray1
strarray2()=split(strarray1(RowNumber-1),",") 'split the selected row up into strarray2 using comma as delimiter
strarray2(ColumnNumber-1)=ReplString ' replace the selected row and column data
strarray1(RowNumber-1)=join(strarray2,",") 'join the selected row back up
strfile=join(strarray1,vbcrlf) ' join the whole file back up
open AFilename for output as #FF
print #FF, strfile ' write it to the file
close #FF
End Sub
-
Aug 17th, 2001, 12:53 AM
#8
Thread Starter
Hyperactive Member
there were two typo errors in the code but were easy fixes.
VB Code:
sub ReplaceNumber(AFilename as String, RowNumber as Integer, ColumnNumber as Integer, RplString as String)
' had tochange RplString with ReplString
sub ReplaceNumber(AFilename as String, RowNumber as Integer, ColumnNumber as Integer, ReplString as String)
'||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\
open SourceFilename for output as #FF
print #FF, strfile
close #FF
' had tochange SourceFilename with AFilename
open AFilename for output as #FF
print #FF, strfile
close #FF
Thanks for the comments I understand it a little now..
Jason
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|