0

In my project; I need to query our database, and then use the result set to generate a fixed width flat file. I'm using a solution based off of this question. All is well and good for 99% of the records, but I am running into one situation that's causing issues:

In the event of "special characters", like ® or ™; it ends up skewing the length of the column when writing to a text file. When I check the results, I get ® in the event of the ® symbol, and â„¢ in the event of ™.

I'd like to keep the garbled characters in the final result (for now), cause it'll get picked up later in the validation/sanitation process. (i.e. in the case of a size like '5½'; I don't want to simply drop the ½ cause then it'll look like '5')

Is there a way to get the actual length of what those special characters will get serialized to so I can take that into account?

EDIT Here's the code that does the text writing:

using (var fileStream = new StreamWriter(fileName, false))
            {
                foreach (
                    var fileRecord in recordTypes.Select(record => record.RetrieveForImport(startDate, endDate)).SelectMany(records => records))
                {
                    try
                    {
                        fileStream.WriteLine(fileRecord.Serialize());
                    }
                    catch (MySerializationException ex)
                    {
                        LogSerializationError(fileRecord, ex.Message);
                    }
                }
            }

And I'm using TextPad to verify the results afterwards.

Community
  • 1
  • 1
Jim B
  • 8,344
  • 10
  • 49
  • 77
  • 1
    Are you reading it is `byte[]` or `char[]`. Also, what encoding? – Brian Warshaw Sep 11 '12 at 12:45
  • It doesn't look like any encoding is explicitly defined; so whatever the default would be. I'm not sure what you mean by reading it in; the data comes back from the database as a DataTable, and I write via a StreamWriter. – Jim B Sep 11 '12 at 12:51
  • 1
    Show us how you are creating the StreamWriter - you are probably using the default UTF8 encoding. – Polyfun Sep 11 '12 at 12:54
  • Also what are you using to view the file? – Polyfun Sep 11 '12 at 12:56
  • Thanks for the suggestions guys, I updated my question with the requested info. – Jim B Sep 11 '12 at 13:35

0 Answers0