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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Open" File Statement Question

Status
Not open for further replies.

bakerm

Programmer
Apr 25, 2000
53
US
Newbie question for you guys....

Is there a way using the Open statement to open a .txt file read one line at a time, modify that line if needed, and write the line back into the location it came from in the file?

Any help is greatly appreciated!




 
Nope. Either load the text file into memory or copy the first file line-by-line into a second.

If you want to do this for INI files, however, there are API calls for that. Note that there is some delay in updating the INI files when these API function are used.

Best regards

 
Certainly!
A great source for accessing files is TEACH YOURSELF VISUAL BASIC 6, by Scott Warner. ( $30.)
The old VB 5 function, OPEN(), has been replaced by the
newer VB6 function .OpenTextFile. Once you open a textfile,
you can review it, line-by-line via .ReadLine
VB5's access of textfiles is both ackward and unreliable.
Upgrade to VB6.
 
DonQuichote is right, not much you can do for text files except what he suggests.

This isn't strictly true, because you could open the text file as a BINARY file and overwrite any bytes you choose. The problem is you can only write back exactly the number of bytes that were there originally. You can't make a new "first line of text" that is longer or shorter than the original. Then there is the problem of trying to locate something like the 256th record of the file. There is no direct way to do this, you'd need to scan the file from the beginning, counting newline markers (vbCrLf) as you go. Yuck!

Of course fixed-length record RANDOM files are another story altogether. They solve all of your problems except when you need to delete a record from the original file. Plus, you need a properly formatted file in the 1st place, and text files almost always are not in the form you need for RANDOM access.

I'm not sure what that commercial for the Scripting.FileSystemObject ("FSO") class was all about. VB6 has the same I/O statements that VB5 did. Yes, there are some issues with the older-style VB I/O. Yes, Microsoft does recommend that people use the FSO instead of native VB I/O for many applications. But the FSO has many limitations that VB I/O can overcome: for example FSO doesn't support COM: port I/O, or RANDOM file I/O, or BINARY I/O. It only handles filesystem stream files.

I use FSO a lot myself, but I use VB I/O almost as often. It can do so much more! And the FSO isn't going to help you edit the first record of a text file either, unless you do something like DonQuichote suggested in the first place.

So much depends on what you are really trying to do.
 

THOMASNG:
Could you explain your statement a little further (the replacement).
When bakerm starts a new VB project and tries to use the replacement, "OpenTextFile" isn't available as a basic function, but "Open" still is. Why, if it is a replacement?

And show us some example(of replacing a line of data) as you are suggesting, that is simpler than doing it the traditional way. [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top