?
Solved

Need To Write String/Number To A File

Posted on 1999-01-05
4
Medium Priority
?
142 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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

770 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