File IO for Text Replacement

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
 
V

Vadym Stetsyak

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
 
G

Guest

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?
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top