File IO for Text Replacement

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Hello, LouArnold!
You wrote on Mon, 2 Oct 2006 11:28:01 -0700:

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

L> I tried the StreamReader and StreamWriter approach and the
L> String.Replace
L> method. It works well, except that not all the file contents gets
L> preserved.
L> Three bytes at the start of the file are omitted. These occur just
L> before the
L> CRLF characters. Can I read the file in a binary way and then use the
L> equivalent 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/library/system.io.binarywriter.aspx )


With best regards, Vadym Stetsyak.
Blog: http://vadmyst.blogspot.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?
 
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.
 
Back
Top