Replacing a single line from a text file 
Author Message
 Replacing a single line from a text file

Hello all,
  I want to replace a single line from a text file.
  I have the line number of which the text should be  
  repleced. How to achieve this ?
  I am using StreamReader and StreamWriter classes.

  Please help,

-Regards,
Dinesh Jain.

*** Sent via Developersdex http://www.*-*-*.com/ ***
Don't just participate in USENET...get rewarded for it!



Sun, 11 Sep 2005 22:53:10 GMT  
 Replacing a single line from a text file

Quote:
> Hello all,
>   I want to replace a single line from a text file.
>   I have the line number of which the text should be
>   repleced. How to achieve this ?
>   I am using StreamReader and StreamWriter classes.

You must copy the content before the line to the new file, then write the
new line and copy the content after the line.

Optimization, only possible if length of new and old line is equal: Store
the byte position within the file when reading so you can use binary access
and write the new line to the same position.

Armin



Sun, 11 Sep 2005 23:39:07 GMT  
 Replacing a single line from a text file


Quote:

> You must copy the content before the line to the new file, then write
> the new line and copy the content after the line.

If the data to be written is the same length as the data being replaced, he
can just overwrite it.  I'm not sure if streams support this however.

--
If you don't like lunchmeat, please remove it from my e-mail address to
send me an e-mail



Mon, 12 Sep 2005 01:54:12 GMT  
 Replacing a single line from a text file

Quote:
> > You must copy the content before the line to the new file,
> > then write the new line and copy the content after the line.

> If the data to be written is the same length as the data being
> replaced, he  can just overwrite it.  

Right, that was the second part of my answer (that you didn't quote). :-)

Quote:
> I'm not sure if streams
> support this however.

Yes, you can set the position property.

Armin



Mon, 12 Sep 2005 02:07:59 GMT  
 Replacing a single line from a text file

Quote:

> Right, that was the second part of my answer (that you didn't quote). :-)

So it was.  Don't know why I missed that.  Brian hiccup I guess.

--
If you don't like lunchmeat, please remove it from my e-mail address to
send me an e-mail



Mon, 12 Sep 2005 03:49:32 GMT  
 Replacing a single line from a text file

Hi ,

Thanks for your valuable replies.
I got the method.
While reading text file(using Stream Reader), I have calculated the byte
position-where to write.
But remember, new line character is of 2 bytes.
That is read each line of text then increment the
counter by string length + 2(end of line).
Then user StreamWriter and seek at that location
and write the string.
The lengths of old string and new string should be
same.
Is it the rigth method ?

-Regards,
Dinesh Jain.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Mon, 12 Sep 2005 12:34:34 GMT  
 Replacing a single line from a text file

Quote:
> Thanks for your valuable replies.
> I got the method.
> While reading text file(using Stream Reader), I have calculated
> the byte position-where to write.
> But remember, new line character is of 2 bytes.
> That is read each line of text then increment the
> counter by string length + 2(end of line).
> Then user StreamWriter and seek at that location
> and write the string.
> The lengths of old string and new string should be
> same.
> Is it the rigth method ?

Sounds good.

Armin



Mon, 12 Sep 2005 18:59:36 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Replace a line in a text file.

2. Replace lines in a text file

3. Find and replace a line in a text file

4. replace line with new line in ini file

5. Putting whole line of text in textfield on single keypress

6. Orientation Control of Single Text Line

7. Reading a single line in a multiline text box

8. Q: Change Color of Single Line in Text Box

9. How to set the curso on the first single line input text box of my page

10. Value single line text box with variable containing special characters

11. need to right justified numbers in a single line text box

12. Replacing text in only the top 13 lines of a document

 

 
Powered by phpBB® Forum Software