Solved

Inserting text into text file

Posted on 2001-06-25
8
182 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How does CurrentUser work? 10 38
Hide vba in gp 7 104
fso.FolderExists("\\server\HiddenFolder$") 4 78
Fastest way to find and count same items VB6 16 22
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…

839 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