Link to home
Start Free TrialLog in
Avatar of airbronedude
airbronedude

asked on

Inserting text into text file

Hello.  I am trying to insert text into my .ini file.  here is my ini file

#
#
#My ini file
#
StartOfUsersMail
soso@nasa.gov
who@nasa.gov
here@nasa.gov
#
#
#

Ok...How do I insert text into my ini between soso@nasa.gov and who@nasa.gov    I don't want put it at the end of the file.  Do I have to write a function to go out and totally rewrite the ini file over???
Thanks
 
     
Avatar of wileecoy
wileecoy
Flag of United States of America image

How does your program track where you want it to be placed?
Avatar of hes
Avatar of simonbennett
simonbennett

Sorry it's brief, but you need to use the SetPrivateProfileString and GetPrivateProfileString

Good Luck

Simon
Yes, you need to rewrite whole file:

Private Sub Command1_Click()
Dim fso As New FileSystemObject, s As String, n As Long
Dim streamS As TextStream, searchS As String, insertS As String
Dim s1 As String, s2 As String, s3 As String
searchS = "soso@nasa.gov"
insertS = "somethign@nasa.gov"
Set streamS = fso.OpenTextFile("c:\test\test.ini")
s = streamS.ReadAll
n = InStr(s, searchS)
s1 = Mid(s, 1, n + Len(searchS) - 1)
s2 = vbCrLf & insertS
s3 = Mid(s, n + Len(searchS))
streamS.Close
Set streamS = fso.OpenTextFile("c:\test\test.ini", ForWriting)
streamS.Write s1 & s2 & s3
End Sub
ASKER CERTIFIED SOLUTION
Avatar of jklmn
jklmn

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
"Yes, you need to rewrite whole file"

Either that or someone else does, such as Windows and the previously mentioned SetPrivateProfileString.

The way textfiles are stored on disk, they are written in consecutive bytes until a sector fills up.  If you want to insert something, there's three ways for the O/S to handle it:

1) Shift all of the bytes forward to make space, then insert
2) Re-write all of the bytes to a new place, then delete the old
3) Replace some characters with a marker that points to the location of the inserted text

#1 is a convenient approach, but is dangerous because if the electricity goes out while writing, your new file isn't done writing, and the original one is now corrupt
#2 is easy to do, but is usually the slowest for writing; with high-speed drives, this is usually the approach of modern O/Ss
#3 is the fasted for writing, but the slowest for reading.  I think MSWord uses a variation of this.

--
So you have the choice of simulating the O/S, or letting the O/S do the work for you!  Your choice.  Pick either jklmn or simonbennett (or maybe hes' link helped.)
Hi airbronedude,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Accept jklmn's comment(s) as an answer.

airbronedude, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will follow up.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
Comment from expert accepted as answer

Computer101
E-E Moderator