If i'm opening a text file for editing and i want to take
text line by line and edit it, how do i put the text back
into the file line by line after I have edited it?
Printable View
If i'm opening a text file for editing and i want to take
text line by line and edit it, how do i put the text back
into the file line by line after I have edited it?
didn't test this code but it should be all you
need to get you going.
Load your file into an array
Edit your lines
then load the array into the file
Open myfile for input as #1
dim i as integer
dim myArray()
Do while not eof(1)
redim preserver myArray(i)
line Input #1,myArr(i)
i = i + 1
loop
close #1
You now have all your lines in an array
to edit line three
text1 = myarr(2)
edit the line then some button
myArr(2) = text1
when done playing
open yourfile for output as #1
for i = lbound(myarr) to ubound(myarr)
print #1, myarr(i)
next
close #1
Code:' Sorry, it's monthend and just because they pay me to work
' they expect me to work...really...lol
'here is a basic version of edit
'need 1 listbox and 1 command button
'List1 Command1
'one file called myFile.txt stored in C:\My Documents
'need one bas module with this code in it
Public myArr()
'******************this is your form code
Option Explicit
Public myNum 'used to store array number based on list1 index
Private Sub Form_Load()
Dim myFile As String
myFile = "C:\my documents\myfile.txt"
Open myFile For Input As #1
Dim i As Integer
Do While Not EOF(1)
ReDim Preserve myArr(i)
Line Input #1, myArr(i)
List1.AddItem myArr(i)
i = i + 1
Loop
Close #1
End Sub
Private Sub Command1_Click()
List1.Clear
Dim i As Integer
Dim myFile As String
myFile = "C:\my documents\myfile.txt"
myArr(myNum) = Text1.Text
Open myFile For Output As #1
For i = LBound(myArr) To UBound(myArr)
Print #1, myArr(i)
Next
Close #1
List1.Clear
'Refresh
Call Form_Load
End Sub
Private Sub List1_Click()
Text1 = List1.Text
myNum = List1.ListIndex
End Sub
Can you dump it into a textbox? then Edit?
if that will work...then:
Create 2 buttons: (a load & a save button)
Create 1 Textbox: (multiline)
if the file doesn't exist yet...an error will occur if you try to load first. So add an error handler if u need it.Code:Private Sub cmdLoad_Click()
Text1 = ""
Dim temp
Open "C:\Data.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, temp
Text1 = Text1 & temp & vbCrLf
Loop
Close #1
End Sub
Private Sub cmdSave_Click()
Open "C:\Data.txt" For Output As #1
Print #1, Text1.Text
Close #1
End Sub
This is plain and simple. :) Hope it helps! :)
All this seems really helpful, unfortunately, I forsee a few problems......
This program needs to parse each line in the text file
and see if a certain string exists in it from a database.
If it does, it is edited accordingly based on other info
from the database.
The database is huge and the text file is about a meg of raw text.
The text file must retain it's structure.
So.......
I'm trying to make it as fast as possible, while retaining
the original structure (line for line) of the text file,
make all this totally automated (no user interaction), and
of course maintain data integrity. Therefore, I'm shakey
about using a listbox as it will automatically alphabetize
the strings and I don't want the user to have to update and
edit anything.
Thank you in advance for your help
P.S. Bababooey
Thank you for all your help.
When I originally encountered this endeavor, I realized
that it would probably be a slow process. I thought that I
would use a method similar HeSaidJoe's method except it
would parse the data line for line without using arrays.
I was just wondering if there was an alternate and faster way.
Since this part of the program is completely automated and
no user input is necessary, I guess I can sacrifice speed at
the expense of accuracy. Thanks again for the help though :)