?
Solved

Need To Write String/Number To A File

Posted on 1999-01-05
4
Medium Priority
?
144 Views
Last Modified: 2010-05-03
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 !
0
Comment
Question by:amyy
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
cyber_bandit earned 200 total points
ID: 1454049
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.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1454050
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

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1454051
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.
0
 
LVL 4

Expert Comment

by:chabaud
ID: 1454052
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
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question