469,964 Members | 1,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

File IO for Text Replacement

I have a need to replace text in a file with new text. The files may or may
not be text files in the sense that there may not be "lines" and they may not
contain printable characters.

I tried the StreamReader and StreamWriter approach and the String.Replace
method. It works well, except that not all the file contents gets preserved.
Three bytes at the start of the file are omitted. These occur just before the
CRLF characters. Can I read the file in a binary way and then use the
equivalent to the String.Replace? The input will no longer be a string
Oct 2 '06 #1
4 1225
Hello, LouArnold!
You wrote on Mon, 2 Oct 2006 11:28:01 -0700:

LI have a need to replace text in a file with new text. The files may
Lor may
Lnot be text files in the sense that there may not be "lines" and they
Lmay not
Lcontain printable characters.

LI tried the StreamReader and StreamWriter approach and the
LString.Replace
Lmethod. It works well, except that not all the file contents gets
Lpreserved.
LThree bytes at the start of the file are omitted. These occur just
Lbefore the
LCRLF characters. Can I read the file in a binary way and then use the
Lequivalent to the String.Replace? The input will no longer be a string

You can create temporary file, and write the contents of original file after
processing them ( replacing the strings you want ).

Did you try BinaryWriter?
( http://msdn2.microsoft.com/en-us/lib...arywriter.aspx )
With best regards, Vadym Stetsyak.
Blog: http://vadmyst.blogspot.com
Oct 2 '06 #2
I read about BinaryReader and BinaryWriter. My question is: can the input be
treated as text? Perhaps a better question is: Can the type of encoding(ie.
UTF-x) be known when its read, if one is to try and treat the input as text?
Can one encode a comparison string to search for and replace with another
string?

If one can't do this then one is stuck with enumerating all the tyoes (ie:
..TXT, .CSV, etc) that may contain text, or all the file types that don't
contain text?
Oct 3 '06 #3
Lou,

If a file does not completely contains Text, than it has to be read binary,
it is than up to yourself to split it in parts and threat those parts that
are text in readable format using the encoding class.

http://msdn.microsoft.com/library/de...ClassTopic.asp

The same has to be done again back of course.

Cor

"LouArnold" <Lo*******@discussions.microsoft.comschreef in bericht
news:71**********************************@microsof t.com...
>I read about BinaryReader and BinaryWriter. My question is: can the input
be
treated as text? Perhaps a better question is: Can the type of
encoding(ie.
UTF-x) be known when its read, if one is to try and treat the input as
text?
Can one encode a comparison string to search for and replace with another
string?

If one can't do this then one is stuck with enumerating all the tyoes (ie:
.TXT, .CSV, etc) that may contain text, or all the file types that don't
contain text?

Oct 3 '06 #4
Thank you, that does answer my question. I will try a few experiments with
Binary input and interpretation as ASCII text with the Binary Reader and
Writer methods. I will get back when I have concluded if this expectedly
simple program will get too complicated because of differing file types.
Oct 5 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by electrixnow | last post: by
8 posts views Thread by John Salerno | last post: by
reply views Thread by doomsday123 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.