Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm trying to read a file, line by 1

Status
Not open for further replies.

cheyney

Programmer
Jul 16, 2002
335
0
0
CA
I'm trying to read a file, line by line, into another file. The reason I do it that way is because I need to replace a certain string with another. Details aside, this is generally what I'm doing.


Open inAppPath & "\reg.reg" For Input As #1
Open inAppPath & "\newreg.reg" For Output As #2

Do Until EOF(1)
Line Input #1, strInput

newstring = Replace(strInput, "#APPPATH#", "c:\")
Print #2, newstring

Loop
Close #1
Close #2


Everything is good, EXCEPT there are two characters at the beginning of the fist line of the new file that don't exist in the old one, namely these two: ÿþ

This renders the new file unreadable in some applications. If I encorporate logic to remove the first two characters of the first line of the file copy, then everything works fine. However I'm hesitant to do this because I don't know where those two characters are coming from. Please help

cheyney
 
Take a look at the TextStream object that is in the Microsoft Scripting Runtime. (scrrun.dll) It has both a ReadLine() and WriteLine() functions.

Something like:
(Set a reference to Microsoft Scripting Runtime)

Private Sub Form_Load()
Dim fs As FileSystemObject
Dim ts1 As TextStream
Dim ts2 As TextStream
Dim newstring As String

Set fs = New FileSystemObject
Set ts1 = fs.OpenTextFile("OldFileName", ForReading, False)
Set ts2 = fs.OpenTextFile("NewFileName", ForWriting, True)

Do While ts1.AtEndOfStream = False
newstring = Replace(ts1.ReadLine(), "#APPPATH#", "c:\")
ts2.WriteLine (newstring)
Loop
ts1.Close
ts2.Close
Set ts1 = Nothing
Set ts2 = Nothing
Set fs = Nothing
End Sub
 
thanks for your reply, that helped...

I still wonder where those characters come from, though...could it just be a VB bug?

cheyney
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top