开发者

stream writer not writing trimmed text to the file in C#

I have a strange problem. I am getting a stream of text from a tcp client and writing it to a file. The stream is not fully filled hence while converting it to string the unfilled parts of the byte array are converted to \0 so i finally end up having,

str = "blah foo bar \0\0\0\0\0...";

so what i did is

str = str.trim('\0');

But if i do this then the string is not getting written to a file using stream writer. If i comment the trim line then its getting written along with all the white space characters. Here is my full code

StreamWriter sw = new StreamWriter("c:\\a\\ta.txt");
while (true)
{
    try
    {
        NetworkStream ns = tc.GetStream();
        byte[] instream = new byte[tc.ReceiveBufferSize];
        Thread.Sleep(2500);
        ns.Read(instream, 0, tc.ReceiveBufferSize);
        string decodedData = string.Empty;
        decodedData = System.Text.Encoding.ASCII.GetString(instream);
        decodedData = decodedData.Trim('\0');
        //string a = "dfdsfdsfdsfdsf";
        //string b = a.Trim('\0');

        try
        {
            sw.Write(decodedData);
            //MessageBox.Show(decodedData);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToS开发者_运维问答tring());
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

Can some one explain me why this is and how i can solve htis out.

oh on debugging i can see that decodedData has the trimmed text neat and clean but i dont know why its not being written to the file.


There are three problems here.

First, you grab the text from the whole array, regardless of how many bytes you actually did receive. Most likely this is the source of your zero characters.

To fix that, change the code as follows:

int actuallyRead = ns.Read(instream, 0, tc.ReceiveBufferSize);
string decodedData = Encoding.ASCII.GetString(instream, 0, actuallyRead);

Secondly, you need to close the stream in order for it to flush its contents. The best way to do that is to wrap it in a using block:

using (StreamWriter sw = new StreamWriter("c:\\a\\ta.txt"))
{
    ... rest of your code here
}

Thirdly, the code would normally never complete. Add a way for it to complete without relying on exception handling, for instance:

int actuallyRead = ns.Read(instream, 0, tc.ReceiveBufferSize);
if (actuallyRead == 0)
    break;
string decodedData = Encoding.ASCII.GetString(instream, 0, actuallyRead);


You're never flushing the writer - I suspect everything's just buffered. You should use a using statement for your StreamWriter, so that it gets disposed when you leave the block. That will then flush the file.

You should also look at the value returned from Stream.Read, and only create a string using the portion of the buffer which has actually been read.

Finally, it's not clear how you expect this to terminate, given that you've got a while(true) loop. You're currently only going to terminate when you get an exception. You should probably terminate if ns.Read returns 0.


Try this:

decodedData = new string(decodedData.ToCharArray());

IIRC, the string constructor will trim trailing NULL terminators.


have you tried...

 decodedData = decodedData.Trim(@"\0");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜