jeudi 9 février 2017

Downloading a simple space delimited file with C# yields garbage characters

Vote count: 0

When trying to initiate a download of a fixed width file in C# and opening the downloaded file with notepad the content comes out complete gibberish. See below as an example.

????????\@@@@@@@@@@@@@@@@@@@@@@@@???????????????????@?????????@????????????@@???????@???????????????????@@@@@@@@@@@??@@@@??@?????????????@@@@@@@@@@@@@@@@?????@@@@@@@@@@@@@@??@@???????@??????????k?????@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@?????????????????????

Here is my code used to perform the download.

char[] buffer = new char[2048];
using (var reader = new StreamReader(responseStream))
{
    using (var tw = new StreamWriter(DESTINATION + subFolder + files[files.Count - 1] + ".txt", false, Encoding.ASCII))
    {
        while (true)
        {
            int readCount = reader.Read(buffer, 0, buffer.Length);
            if (readCount == 0) break;
            tw.Write(buffer, 0, readCount);
        }
        responseStream.Close();
        tw.Close();
    }
}

I'm inclined to say it has something to do with the file encoding.

I've written the same downloader in python and the file downloads as expected with that code. I just can't figure it out with C#.

asked 38 secs ago

Let's block ads! (Why?)



Downloading a simple space delimited file with C# yields garbage characters

Aucun commentaire:

Enregistrer un commentaire