473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

simple text file question

hello,

i want to search a text file for an occurrence of a string and replace some
text after the match, then save the file.

eg: if the file is:

<start>
Hello my name is tony, I am 22.
Hello, my name is sonia, I am 18.
Hello, my name is debbie, I am 8.
<end>

I would like to pass in a search string of "I am " and I also want to be
able to increment each of the ages by 1, then save the file.

So the output file would be:

<start>
Hello my name is tony, I am 23.
Hello, my name is sonia, I am 19.
Hello, my name is debbie, I am 9.
<end>

What is the best way to do this?

Many thanks.
Nov 15 '05 #1
4 2193
"suzy" <me@nospam.com> wrote:
hello,

i want to search a text file for an occurrence of a string and replace some
text after the match, then save the file.
Load the whole file into a string, and use string.Replace().
eg: if the file is:

<start>
Hello my name is tony, I am 22.
Hello, my name is sonia, I am 18.
Hello, my name is debbie, I am 8.
<end>

I would like to pass in a search string of "I am " and I also want to be
able to increment each of the ages by 1, then save the file.
After loading the file into a string, I guess you could iterate
through the string until you find a digit. Make sure this digit is
preceeded by a *delimiter*. Keep iterating until you find another
*delimiter*. Next, piece together a string starting from the first
digit to the last. Convert this to a number and increment it. Remove
the old number from the string. Insert the new one in the same place.
So the output file would be:

<start>
Hello my name is tony, I am 23.
Hello, my name is sonia, I am 19.
Hello, my name is debbie, I am 9.
<end>

What is the best way to do this?

Many thanks.


Hope that helps.
Nov 15 '05 #2
By the way, I should mention that I know I can do a loop with a ReadLine()
iteration, but the text I want to change is likely to be at the end of the
text file with a line preceding it
(contents of that line will be "***DETAILS***").

Is there anyway, I can search from this position onwards rather than start
looping from the top of the file.

"suzy" <me@nospam.com> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...
hello,

i want to search a text file for an occurrence of a string and replace some text after the match, then save the file.

eg: if the file is:

<start>
Hello my name is tony, I am 22.
Hello, my name is sonia, I am 18.
Hello, my name is debbie, I am 8.
<end>

I would like to pass in a search string of "I am " and I also want to be
able to increment each of the ages by 1, then save the file.

So the output file would be:

<start>
Hello my name is tony, I am 23.
Hello, my name is sonia, I am 19.
Hello, my name is debbie, I am 9.
<end>

What is the best way to do this?

Many thanks.


Nov 15 '05 #3
Why not use FileStream.ReadByte to go through the file looking for "D". When
you find one check for "DETAILS" if its not "DETAILS" then continue with
ReadByte..

G.
"suzy" <me@nospam.com> wrote in message
news:Om**************@TK2MSFTNGP11.phx.gbl...
By the way, I should mention that I know I can do a loop with a ReadLine()
iteration, but the text I want to change is likely to be at the end of the
text file with a line preceding it
(contents of that line will be "***DETAILS***").

Is there anyway, I can search from this position onwards rather than start
looping from the top of the file.

"suzy" <me@nospam.com> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...
hello,

i want to search a text file for an occurrence of a string and replace

some
text after the match, then save the file.

eg: if the file is:

<start>
Hello my name is tony, I am 22.
Hello, my name is sonia, I am 18.
Hello, my name is debbie, I am 8.
<end>

I would like to pass in a search string of "I am " and I also want to be
able to increment each of the ages by 1, then save the file.

So the output file would be:

<start>
Hello my name is tony, I am 23.
Hello, my name is sonia, I am 19.
Hello, my name is debbie, I am 9.
<end>

What is the best way to do this?

Many thanks.


Nov 15 '05 #4
Truth is, there are many ways to do this. If performance isn't an issue, I
would just read the entire file into a string (I believe StreamReader has a
ReadToEnd method) and then search the string. First you'd do an
IndexOf("***DETAILS***") and then start reading from there, getting the
number between "I am " and "." by using IndexOf and Substring. Then you
could use Convert.ToInt32 to increment. Of course, you could use regular
expressions (maybe some extra points for that) but that would be beyond my
skills.

Chris

"suzy" <me@nospam.com> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...
hello,

i want to search a text file for an occurrence of a string and replace some text after the match, then save the file.

eg: if the file is:

<start>
Hello my name is tony, I am 22.
Hello, my name is sonia, I am 18.
Hello, my name is debbie, I am 8.
<end>

I would like to pass in a search string of "I am " and I also want to be
able to increment each of the ages by 1, then save the file.

So the output file would be:

<start>
Hello my name is tony, I am 23.
Hello, my name is sonia, I am 19.
Hello, my name is debbie, I am 9.
<end>

What is the best way to do this?

Many thanks.

Nov 15 '05 #5

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

Similar topics

7
by: dan | last post by:
hey peeps, i am completely new at c++ and i need some help with an assignment. it is basically about file i/o with fstreams. i understand how to open a file with fstream, but how would you read,...
13
by: LRW | last post by:
Having a problem getting a onSubmit function to work, to where it popsup a confirmation depending on which radiobutton is selected. Here's what I have: function checkdel() { if...
1
by: Proteus | last post by:
Any help appreciated on a small perl project I need to write for educator/teaching purposes. I have not programmed perl for some time, need to get up to speed, maybe some kind souls hrere will help...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
10
by: serge calderara | last post by:
Dear all, I need to build a web application which will contains articles (long or short) I was wondering on what is the correct way to retrive those article on web page. In orther words, when...
6
by: Jim M | last post by:
I've been distributing a fairly mature, very specific MS Access application to end users in small offices of colleges for several years now. This is a part-time venture and low volume operation-...
3
by: o.o.Pb.o.o | last post by:
The title is pretty vague, I couldn't agree on how to word my inquiry correctly, but I can explain. I'm writing a simple wiki script that is contained in one HTML file (yes, it's been done before)....
4
by: SM | last post by:
Hello, I have a simple question, but can't find the answer. I have a string that contains a path to a file I want to add another string to the end of that string So, if i have : path =...
5
by: fussfart | last post by:
I'm trying to do something that should be very simple but isn't working! (I also want to do something somewhat more complicated, but that has to wait until I figure out the simple stuff.) First, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.