Link to home
Start Free TrialLog in
Avatar of amyy
amyy

asked on

Need To Write String/Number To A File

Hello, I am trying to write a "string/number" combination to a file. Like "Hello=12". I need to know where "Hello=10" is in the file so I can change this to "Hello=12". I am using Visual Basic 5.0 and am very lacking (novice) for now. Thank You for you time !
ASKER CERTIFIED SOLUTION
Avatar of cyber_bandit
cyber_bandit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of caraf_g
caraf_g

Hello amyy

Cyber Bandit's answer is probably not what you're looking for as he doesn't give you a method to find the text in the file and replace it.

The following does that for you:

Private Sub Command1_Click()

Dim strW As String
Dim intLen As Integer
Dim intPos As Integer

'First, read the contents of the file into memory
intLen = FileLen("C:\Fred.txt")

Open "C:\Fred.txt" For Input As #1
strW = Input(intLen, #1)
Close #1

'Then find "Hello=10"
intPos = InStr(strW, "Hello=10")

'If found, replace with "Hello=12"
If intPos = 0 Then
    Exit Sub
End If

strW = Left(strW, intPos - 1) & _
       "Hello=12" & _
       Right(strW, Len(strW) - intPos - Len("Hello=10") + 1)

Open "C:\Fred.txt" For Output As #1
Print #1, strW;
Close #1

End Sub

Basically what the above file does is read the complete file into one long big string.

Once that is done, the program uses the Instr function to locate the string "Hello=10" within that big string.

Let's say, that your file contains
blahblahbeforeHello=10blahblahafter

it breaks the string up into blahblahbefore and blahblahafter and rebuilds it as

blahblahbeforeHello=12blahblahafter

Then it writes the whole lot back into the file, overwriting anything that might already be there.

The net result of the whole thing is that it seems as if "Hello=10" is replaced with "Hello=12" in your file.

PS - Proper file handling dictates the use of "FreeFile" for opening and closing files but I've not done that here as it might confuse matters - er... how novice are you exactly?
I also haven't catered for multiple occurrences of "Hello=10"; that would complicate matters even further. But if you're interested, let me know.
Your text file looks like a .ini file with key=value lines; just append sections in the file and...

If so, you could use standard API functions to access it using the following code:

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

private sub Command_click()
  'this command replace the current Hello=10 by Hello=12
  'in the file, or create the line if it not exists.
  WritePrivateProfileString "theSection", "Hello", "12", "myfile.txt"
end sub