Reading from & writing into text file

Can anyone tell me how to open a text file both in read & write modes. I need to scan line by line in the text file & need to make changes in each line then write back to the same text file.
Preferrably using open # statement.
kbalarajuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VbmasterCommented:
You can't have a ordinary text file open in both read&write. What you can do is open the textfile, open a temporary file and for each line read the data from the textfile, do any changes if neccessary and then save it to the temporary file. When all lines are done, you close both files. Then you do the Kill statement on the textfile and use the Name statement to rename the temporary file to whatever the textfile was called.
0
VbmasterCommented:
Here's the code:

  Dim Rad As String
  Dim SourceFile As String
  Dim SourceFileNr As Integer
  Dim TempFile As String
  Dim TempFileNr As Integer
 
  SourceFile = "D:\A.txt"
  TempFile = "C:\A.txt"
 
  SourceFileNr = FreeFile
  Open SourceFile For Input As #SourceFileNr
  TempFileNr = FreeFile
  Open TempFile For Output As #TempFileNr
  Do Until EOF(SourceFileNr)
    Line Input #SourceFileNr, Rad
    'Do any changes on the Rad string
    Print #TempFileNr, Rad
  Loop
  Close #TempFileNr
  Close #SourceFileNr
 
  Kill SourceFile
  Name TempFile As SourceFile
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VbmasterCommented:
TempFile = SourceFile & ".bak"

is probably a better solution
0
kbalarajuAuthor Commented:
Can we open a text file having size of more than 100MB using open & work with it! Do we have any size limitaions while working with them???

Thanks,
Raju
0
VbmasterCommented:
The Seek command which is used to get the current position within a file is limited to 2Gb, so any size up to 2Gb should work fine.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.