473,799 Members | 2,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

edit a text file

Hi!

I need to edit a text file.
I need to replace a given text in the file with a new one.
This text can exist anywhere in the file, so i first need to locate the text
position and then edit it.
How do i acheive this.

Abhishek
Nov 16 '05 #1
4 10153
Hi Abhishek,

Read the entire file into a string, edit the string, and write it all back
to the file.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
hi,

depending of the size of the file you can do two things:

1- if the file is small read it completely in a string and use
String.Replace , write the file back
2- if file is big , read it line by line, replace string and write to a temp
file, at the end overwrite the original file

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Abhishek" <ab******@alway shelpful.com> wrote in message
news:ui******** ******@TK2MSFTN GP14.phx.gbl...
Hi!

I need to edit a text file.
I need to replace a given text in the file with a new one.
This text can exist anywhere in the file, so i first need to locate the
text
position and then edit it.
How do i acheive this.

Abhishek

Nov 16 '05 #3
I> 2- if file is big , read it line by line, replace string and write to
I> a temp
I> file, at the end overwrite the original file

If the file is big, you cannot read it line by line, just because there may
be only one line of text, all that big file, and you'll either gain an OutOfMemoryExce ption
or degrade the performance dramatically. Should do processing in a streaming
fashion with a lookahead of at least the search term in size if you expect
large sizes.

--
Serge
Nov 16 '05 #4
Hi Serge,

You are 100% right, my mistake.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Serge Baltic" <ns*@hypersw.ne t> wrote in message
news:c8******** *************** ***@news.micros oft.com...
I> 2- if file is big , read it line by line, replace string and write to
I> a temp
I> file, at the end overwrite the original file

If the file is big, you cannot read it line by line, just because there
may be only one line of text, all that big file, and you'll either gain an
OutOfMemoryExce ption or degrade the performance dramatically. Should do
processing in a streaming fashion with a lookahead of at least the search
term in size if you expect large sizes.

--
Serge

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4275
by: matt | last post by:
I am using a text file as a database, each field delimited by || I want to be able to print all lines to a page, then by selecting one (with, say, a radio button) it will load into the form at the bottom of the page, where I can edit the fields, and save back to the same line in the text file. I dont know how to use primary keys or anything with a text file database, any help in this would be appreciated! Thanks matt
4
15574
by: Option^Explicit | last post by:
What I'm trying to do: Open a text file and display the contents in a text box (I've done this) Need to be able to edit the file from within the textbox and have it save back to the source file.(can't figure how to do this) I was thinking on the Open for Output or Append, but I can't get it to accept a variable(TextBoxText) to open(since it's not a file) , what would be the simplest way to accomplish this task?
0
3119
by: Alex | last post by:
Interested in more .NET stuff visit www.dedicatedsolutions.co.uk The DataList is not as powerful as the DataGrid. It requires more work from you since it has no default data presentation format. However, the DataGrid begins to get very cumbersome as the number of columns of data you present increases. Anything more than half a dozen columns or so and you probably induce horizontal scrolling - a real no-no for me. If you put such a...
0
1769
by: Alex | last post by:
Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Configuration Public Class Main : Inherits Page Private strConn As String =
4
3720
by: Glenn M | last post by:
I have a shared XML file on a server . i also have one xslt file that performs a simple transform on in to view the data. now i want to have another page that lets users modify the shared xml file via some editable controls such as text boxes , option boxes etc. how can i implment this , should i use another xslt file with <INPUT> controls . if so how can i save the result back using the asp.net
2
4176
by: Sin | last post by:
Hello everyone, I'm totally stumped at how little info I can find in MSDN or on the web concerning this. It's almost as if only microsoft personel hold the key to these secrets or something!!! I'm currently writting a program in which there is a file/folder list which mimics part of Explorer's list. I've got it pretty much cornered, but I still have a couple of problems :
1
2168
by: ollielaroo | last post by:
Hi guys, Firstly I did do a search for this one first but I couldn't find anything related in this forum. I am using Dreamweaver MX and trying to build admin pages for an ASP site. My problem is I have Categories and various Products in each Category. I'm trying to build a page to EDIT/UPDATE each product.I want to be able to change the Category that a product belongs to, but all I get in the drop-down menu is the one specific Category. ...
1
2137
by: Xicon | last post by:
I am looking to create a program that is able to edit a text file that is not located within the program. This particular text file is always in the exact same location and is always named the exact same, and I would like to find a way to make it easily editable. How do I edit the text file (a single line needs to be changed) using a Visual Basic 2008 program? Please help me, I will be VERY appreciative! EDIT: I use Visual Basic 2008...
0
1296
by: yemen2007 | last post by:
hi i have some problem in my project i want help . the problem it is update or edit to the file in xml.my project is write and update/edit to xml file. i done the first one is write bout i have problem in the update/edit function . i write some value inside file i want change the value that one in the file. i use c# so could u plz help me in this function. this the code for write : XmlTextWriter f = new XmlTextWriter("website...
0
9689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10269
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.