Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
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:
 
 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 14-Jan-13 3:37am
Comments
Alan N at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

When I try your code here:
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 | :laugh:
  Permalink  
Comments
Vipin_Arora at 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 at 16-Jan-13 2:03am
   
Exactly what mode value are you using?
OriginalGriff at 16-Jan-13 3:27am
   
Do I assume from the deleted comment that you found it wasn't? :laugh:
Vipin_Arora at 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 at 16-Jan-13 4:00am
   
Its not: FileMode.Create.
It is: FileMode.Append
OriginalGriff at 16-Jan-13 4:17am
   
There is your problem!
When you use Append the existing content of the file is preserved - so when you write your data, if what you write is shorted than the existing file then the data that was there previously will be left behind at the end of the "new" file.
I would suggest that you might want to rename the original to a backup name (deleting any old copy perhaps) and then use Create instead to ensure you get a clean file.
Vipin_Arora at 16-Jan-13 23:55pm
   
The new data is appended at the end of the file, not overwriting the existing content.

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100