views:

35

answers:

1

I have very little experience with VB6 and I need some help. I'm trying to read from file "A" one line at a time, make changes to that line, and write it back out to file "B". Appending File "B" as I go. I've found plenty of info on the web but none of it matches what I'm looking for.

If there's anyone who knows of a good link or can show me some code I'd greatly appreciate it.

Thanks

+1  A: 

I threw this together real quick, but it should be pretty easy to read. You open your file to read, and your file to write to, and iterate through it.

Dim fileIn As Integer
Dim fileOut As Integer
Dim sLine As String

fileIn = FreeFile

Open "C:\Temp\input.txt" For Input As fileIn

fileOut = FreeFile

Open "C:\Temp\output.txt" For Append As fileOut

Do While Not EOF(fileIn)
    Line Input #fileIn, sLine
    sLine = sLine & " has been changed"  ' This is where you'd make your changes
    Print #fileOut, sLine
Loop

Close fileIn
Close fileOut
LittleBobbyTables
That is EXACTLY what I was looking for! Thank you so much.
JimDel
Glad I could help!
LittleBobbyTables
Don't use that "FreeFile + 1" idea. FreeFile() is a function that returns an available slot in VB6's file control block table. Adding one to the value risks colliding with a file number already in use.
Bob Riemersma
@Bob -- Thanks for pointing that out, I forgot about that. I've edited my answer in response.
LittleBobbyTables
`Close fileNum` -> `Close fileIn`
wqw
Thanks for the edit.
JimDel
This can also be a little faster if you tell VB6 to use its maximum sequential I/O buffer size. Add Len = 32767 to the end of the Open statements.
Bob Riemersma