Solved

Inserting text into text file

Posted on 2001-06-25
8
178 Views
Last Modified: 2010-05-18
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
 
     
0
Comment
Question by:airbronedude
8 Comments
 
LVL 4

Expert Comment

by:wileecoy
ID: 6225237
How does your program track where you want it to be placed?
0
 
LVL 20

Expert Comment

by:hes
ID: 6225293
0
 
LVL 6

Expert Comment

by:simonbennett
ID: 6225299
Sorry it's brief, but you need to use the SetPrivateProfileString and GetPrivateProfileString

Good Luck

Simon
0
 
LVL 5

Expert Comment

by:jklmn
ID: 6225402
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Accepted Solution

by:
jklmn earned 50 total points
ID: 6225410
Ooops, you need to close the file:
streamS.Close
after
streamS.Write s1 & s2 & s3
and before End sub.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6225541
"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.)
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7139803
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
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7178887
Comment from expert accepted as answer

Computer101
E-E Moderator
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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.
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

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

18 Experts available now in Live!

Get 1:1 Help Now