Strange characters after reading and writing a textfile

This week I helped out my Business Intelligence colleagues again.

After tackling another problem with a regular expression, the end result contained strange characters. In the output below there is a question mark icon where the Pound (£) sign used to be.

"Whatever returned, � 35.00 debited";3;1;"";0;0

The problem is that the system where the code executes uses a different code page. This messes with the encoding. To solve this we used the Enconding.Default as parameter when reading or writing the text file.

string input = File.ReadAllText(sourceFile, Encoding.Default);
// pattern removed for simplicity
string output = Regex.Replace(input, pattern, " ");
File.WriteAllText(destinationFile, output, Encoding.Default);

About erictummers

My work as a recruited developer changes almost every month. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s