[VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line #
Go Back  Xtreme Visual Basic Talk > > > [VB.Net 2010 Express] Replace Text File Text on Line #


Reply
 
Thread Tools Display Modes
  #1  
Old 10-02-2011, 09:04 PM
Shane74340 Shane74340 is offline
Newcomer
 
Join Date: Oct 2011
Posts: 2
Default [VB.Net 2010 Express] Replace Text File Text on Line #


I am trying to figure out how to replace text in a text file on a specific line number. (for testing, line number 4).

Any ideas?

Thanks,
Shane.
Reply With Quote
  #2  
Old 10-03-2011, 03:06 AM
Flyguy's Avatar
Flyguy[VB.Net 2010 Express] Replace Text File Text on Line # Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 19,160
Default

You can not replace a single line in a text file without rewriting the complete file.
Reply With Quote
  #3  
Old 10-03-2011, 06:23 AM
AtmaWeapon's Avatar
AtmaWeapon[VB.Net 2010 Express] Replace Text File Text on Line # AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

In other words, you have to open the file, write up to the line you want to replace, write the new text, then write the remaining lines. I was actually kind of shocked to find this out but when you think on it it's sensible from the hard drive's perspective.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 10-03-2011, 08:47 AM
Shane74340 Shane74340 is offline
Newcomer
 
Join Date: Oct 2011
Posts: 2
Default

I'm shocked that programming can't do this. However, I agree that it's sensible from the perspective of the Hard Drive.

I'll experiment with this later today.

Thanks,
Shane.
Reply With Quote
  #5  
Old 10-03-2011, 09:31 AM
Flyguy's Avatar
Flyguy[VB.Net 2010 Express] Replace Text File Text on Line # Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 19,160
Default

You can do it if every line in the text file has exact the same line. Then you can open the file, jump to the correct position and write the new data.
Has nothing to do with the programming language.
Reply With Quote
  #6  
Old 03-04-2012, 05:47 PM
unclebill unclebill is offline
Junior Contributor
 
Join Date: Apr 2003
Location: San Diego, CA
Posts: 308
Default

Quote:
Originally Posted by Shane74340 View Post
I'm shocked that programming can't do this. However, I agree that it's sensible from the perspective of the Hard Drive.

I'll experiment with this later today.

Thanks,
Shane.
Programming can do it but it is dependent upon the way the file is organized.

Sequential access is most efficient from a storage perspective in that only the required space is used for each line. The disadvantage is that in order to replace an existing line with a new one, you must read each line from the old file and write it to a new temporary file until the line to be replaced is read. The new data is written to the new file instead of the info read from the original file and the remaining data is read from the original and written to the new file until all data is transferred, Then the old file is typically deleted and the new file renamed to replace the old file.

Random access is more efficient from the perspective of accessing and updating data. Each record contains the same number of bytes thus if the record length is 100 bytes, i.e., every record in the file is 100 bytes long, a line of 1 character or 5 characters uses as much storage as a line with 100 bytes; inefficient from this perspective.

The efficiency comes from the flexibility to access and update the data.
In your example, you mentioned changing line 4. In a random access file, you would simply read line 4, change the contents of the variable containing the line four data and write record 4 of the file with the new data.

Another inefficiency is that if you want to delete line 4, you can rewrite the record as a blank but you cannot erase the record from the file unless you use the approach similar to that for sequential access files, i.e., copy all records to be retained to a new file, delete old file and rename new file.
Reply With Quote
  #7  
Old 03-08-2012, 04:59 PM
Gruff's Avatar
Gruff[VB.Net 2010 Express] Replace Text File Text on Line # Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Back in the stone age when memory was tiny we had to get a little more inventive.

Opening a file for binary could be leveraged with a file mapping system to give you the equivalent of random access without having to have equal sized records. Your map could be in a second file or bundled into the file with the data as a file header.

It was great for quickly retrieving an oddly sized record from the middle of a file. while using just a tad of ram.

What you need to know is that historically file data could be stored in several different mediums. Punch cards, Punched Paper Tape, Reel to Reel tape, Magnetic Disk, Digitally, Etc... At one point someone even came up with storing data on paper as 3D bar code dots.

Essentially files are simply rows of bytes. They do not automatically adust their size in their medium should you want to insert some text into the middle.

This is why everyone here is saying you need to read the file into memory make your adjustments and write the altered data back to disk. You can append to the end of a file without having to read it, but that is about the only exception to the rule.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 03-08-2012 at 05:32 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
[VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line # [VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line #
 
[VB.Net 2010 Express] Replace Text File Text on Line #
[VB.Net 2010 Express] Replace Text File Text on Line #
 
-->