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 !
Who is Participating?
cyber_banditConnect With a Mentor Commented:
Open "myfile.txt" as #1 for input
Print #1, "hello=10"
Close #1
' this will open a file and write the requested text to it

Open "myfile.txt" as #1 for output
Line Input #1, mytext
Close #1
' That is read the first line of the text in, each Line Input statement after the 1st line will read the next line of the file in.

Do some simple string manipulation to change the 10 to 12 and then reopen the file and write it back.
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

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


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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.