Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using BinaryWriter in C#.
It adds junk characters at the end of data.
I have also close and flush the binarywriter.

Here is code snippet:
C#
 using (FileStream objFileStream = new FileStream(sFilePath, fMode))
{
      using (BinaryWriter objStreamWriter = new BinaryWriter(objFileStream))
          {
                        // Write Char String Data to the OCE file
                        //objStreamWriter.Write(sDataToWrite.ToCharArray(), 0, sDataToWrite.Length);
                        objStreamWriter.Write(sDataToWrite.ToCharArray());                        objStreamWriter.Flush();
                        objStreamWriter.Close();
                        
                    }
                    objFileStream.Close();
                }


Please suggest the solution.
Posted
Comments
Alan N 14-Jan-13 9:04am    
Do you have an example of what you mean by "junk" and the input string that produced it. It would also be helpful to see the intermediate character array.
Vipin_Arora 14-Jan-13 10:10am    
junk characters are like:

6A¦''?LP¤¹¾”¬Ì»†Oæl(#©¤-–<‘ô§ÆÛ—švpÜu¥ãž”¸”‡¤4î=)1íGAŠSÖšN:΁ÏZöàR~'ð¥Æ{ÄÑÓ°¥öüai‡ƒÎsFqG^1Iß­(è}(è;P>”¤sŠFéïô£¶I£4ޝE;©ëùRtíH9Ï ~§ëIüx'ŠQŒ›ëJõ¤ÉÍ)¸¤$t=¨'èsA Û4‡Ž£ó§/AÁçÞ—qÏåM¥Ï°üé¹çÿ

1 solution

When I try your code here:
C#
string sFilePath = @"D:\Temp\BBbinwriter.hex";
FileMode fMode = FileMode.Create;
string sDataToWrite = File.ReadAllText(@"D:\Temp\101.txt");
using (FileStream objFileStream = new FileStream(sFilePath, fMode))
    {
    using (BinaryWriter objStreamWriter = new BinaryWriter(objFileStream))
        {
        // Write Char String Data to the OCE file
        //objStreamWriter.Write(sDataToWrite.ToCharArray(), 0, sDataToWrite.Length);
        objStreamWriter.Write(sDataToWrite.ToCharArray()); objStreamWriter.Flush();
        objStreamWriter.Close();

        }
    objFileStream.Close();
    }
I get two identical files - the same length, the same data. What am I doing that you aren't, or you doing that I'm not?
I can only assume that it is a data problem, and your sDataToWrite does not contain what you think it does...:laugh:
 
Share this answer
 
Comments
Vipin_Arora 15-Jan-13 23:17pm    
But it is the case only on one machine (64-bit win2008 server), on other machines (64-bit win2008 server), it is working fine
OriginalGriff 16-Jan-13 2:03am    
Exactly what mode value are you using?
OriginalGriff 16-Jan-13 3:27am    
Do I assume from the deleted comment that you found it wasn't? :laugh:
Vipin_Arora 16-Jan-13 4:03am    
I m sorry, previously I wrote create mode mistakenly. thats why I delete it. I cross check the same and it is actually:Append
Vipin_Arora 16-Jan-13 4:00am    
Its not: FileMode.Create.
It is: FileMode.Append

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900