Solved

Need To Write String/Number To A File

Posted on 1999-01-05
4
136 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 100 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now