1

There might be something obvious I'm missing here, but I can't seem to set the encoding on my FileStream read. Here's the code:

FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
            using (fs)
            {

                byte[] buffer = new byte[chunk];
                fs.Seek(chunk, SeekOrigin.Begin);
                int bytesRead = fs.Read(buffer, 0, chunk);
                while (bytesRead > 0)
                {
                    ProcessChunk(buffer, bytesRead, database, id);
                    bytesRead = fs.Read(buffer, 0, chunk);
                }

            }
            fs.Close();

Where ProcessChunk saves the read values to objects which are then serialized to XML, but the characters read appear wrong. The encoding needs to be 1250. I haven't seen an option to add the encoding to the FileStream. What am I missing here?

4
  • try to use StreamWriter instead of FileStream
    – tym32167
    Commented Nov 24, 2016 at 16:30
  • 2
    Since you're reading bytes, there is no encoding. If those bytes make up text, it is the process of converting those bytes into text that requires an encoder. There is no trace of text processing in your code or question (other than "where do I specify encoder"), so the question is: Do you need an encoder? Commented Nov 24, 2016 at 16:47
  • The characters are mangled by ProcessChunk whose code is missing. A FileStream only returns bytes Commented Sep 1, 2020 at 14:58
  • Please post the code for ProcessChunk. How does it convert bytes to text? You can avoid the problem if you use the 1250 encoding, either in a StreamReader or with Encoding.GetString. You can get it with Encoding.GetEncoding(1250) Commented Sep 2, 2020 at 9:10

2 Answers 2

7

Rather than FileStream, use StreamReader. It has several constructors which allow you to specify the Encoding. For example:

StreamReader sr = new StreamReader(file, System.Text.Encoding.ASCII);

Since you require encoding 1250, this can be done with:

StreamReader sr = new StreamReader(file, System.Text.Encoding.GetEncoding(1250));

I would also suggest writing it as:

using (StreamReader sr = new StreamReader ...etc)

rather than declaring the variable outside the using; and you don't need to do the Close outside the using, since the Dispose will handle that.

5
  • How would I specify the chunk size using Streamreader? I need it to start at the specified start point in the file and read chunks of defined size one at a time, that's why I use FileStream.
    – Flopn
    Commented Nov 24, 2016 at 17:12
  • There is an equivalent 'Read' method on StreamReader msdn.microsoft.com/en-us/library/9kstw824(v=vs.110).aspx ; it's just that now the Encoding is resolved, you have "char"s instead of "byte"s. Commented Nov 24, 2016 at 18:10
  • @Flopn you haven't posted the code for ProcessChunk. That's what's mangling the code. For people to tell you how to use a StreamReader with it they need to know what it does Commented Sep 1, 2020 at 14:59
  • @Richardissimo Encoding.ASCII is the 7-bit US ASCII codepage. This will mangle the characters just like the current code does - the 7-bit range is the same in almost all codepages, it's the values >127 that differ and get mangled Commented Sep 1, 2020 at 15:01
  • @Richardissimo and reproduces the original bug. Use the correct encoding instead. Encoding.GetEncoding(1250) Commented Sep 2, 2020 at 7:42
3

You can also use both FileStream and FileReader :

using (FileStream fs = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding(1252)))
    {                                        
        while (!sr.EndOfStream)
            ProcessLine(sr.ReadLine());
    }
}
6
  • The question asked for 1250, not 1250 Commented Sep 2, 2020 at 7:43
  • 1
    @PanagiotisKanavos I think everyone is capable of adapting code for such a simple thing ? ;-) 1252 is the Encoding set for Windows, so very common.
    – Elo
    Commented Sep 2, 2020 at 9:02
  • Its' even easier to fix the answer to match the question, making it a better match Commented Sep 2, 2020 at 9:03
  • Sorry I upvoted prematurely then. It's not a joke either, as 1252 is not the encoding set of Windows. It's the encoding used in certain Western European languages. Using it with Central or Eastern Europe text will mangle it, which is precisely what the OP's problem is. 1250 is the encoding used in Eastern Europe Commented Sep 2, 2020 at 9:05
  • Which means, right now neither answer solves the original problem (although the other answer manages to lose data too). Commented Sep 2, 2020 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.