Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Inserting text into text file

Posted on 2001-06-25
8
Medium Priority
?
189 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
[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
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
Independent Software Vendors: 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!

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

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

730 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