|
-
Apr 17th, 2008, 07:15 PM
#1
Thread Starter
Member
Changing the saved info in a Text File
Let's say for example, some info's been saved using this code:
Open "C:\Textfile.txt" For Append As #3
Write #3, Text1, Text2, Combo1.text, List1.text
Close #3
and the text file is something like:
Code:
"James", "16", "Green", "1990"
"Bob", "20", "Red", "1980"
How can change an specific info in the text file? (For example, the user enters "Bob" as his name in my program and changes "Red" to something else in a combobox. So i need the computer to find "Bob" in the text file and change "Red" to something the user enters in my program.)
If any more info is required, tell me and i will add.
Asap & thnk you
Last edited by incognito_301; Apr 17th, 2008 at 07:20 PM.
-
Apr 17th, 2008, 11:33 PM
#2
Hyperactive Member
Re: Changing the saved info in a Text File
-
Apr 17th, 2008, 11:53 PM
#3
Re: Changing the saved info in a Text File
With files that are sequentially written and with variable length records, you'll have to read all the file, search for "Bob", make the necessary change and then re-write the complete file. If you have many records it's easier to use a simple Database.
-
Apr 18th, 2008, 04:54 AM
#4
Addicted Member
Re: Changing the saved info in a Text File
here's the way...
vb Code:
Dim toAddLine As String 'the string to be replace
Dim dNewStr(0 To 3) As String 'can be adjusted : for the as if field
Dim dLineSplit(0 To 1) As String 'as a storage of the text file data per row
Dim nF As Integer 'for the filenumber
Dim arrey() As String 'for split
nF = FreeFile
Open "D:\samp.txt" For Input As #nF
w = 0
Do Until EOF(nF)
Line Input #nF, dLineSplit(w)
w = w + 1
Loop
Close #nF
nF = FreeFile
Open "D:\samp.txt" For Output As #nF
For y = 0 To UBound(dLineSplit)
arrey = Split(Trim(dLineSplit(y)), ",")
For x = 0 To UBound(arrey)
If Trim(arrey(x)) = Me.Text_param.Text Then
toAddLine = Me.Text1.Text 'the new value
Else: toAddLine = arrey(x)
End If
dNewStr(x) = Replace(toAddLine, Chr(34), "")
Next
Write #nF, dNewStr(0), dNewStr(1), dNewStr(2), dNewStr(3)
dNewStr(0) = "" 'clear all arrays
dNewStr(1) = ""
dNewStr(2) = ""
dNewStr(3) = ""
Next
Close #nF
'must have:
'Text_param - the parameter of value to be update
'Text1 - the new value
__________________________
if do you think the problem is solved, please mark your post as RESOLVED and please do rate.
Last edited by ThorSubak; Apr 18th, 2008 at 05:01 AM.
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
|