tags:

views:

220

answers:

6

Hi How can i write all bits of a file using c#? For example writing 0 to all bits

Please provide me with a sample

+1  A: 

read into a byte and then test against >= powers of 2 to get each of the bits in that byte

PeanutPower
I believe he wants to set all bits of a file to 0, not read them.
NickLarsen
+2  A: 

Have a look at System.IO.FileInfo; you'll need to open a writable stream for the file you're interested in and then write however many bytes (with value 0 in your example) to it as there are in the file already (which you can ascertain via FileInfo.Length). Be sure to dispose of the stream once you're done with it – using constructs are useful for this purpose.

Will Vousden
+2  A: 

Consider using the BinaryWriter available in the .NET framework

using(BinaryWriter binWriter =
            new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            binWriter.Write("Hello world");

        }
p.campbell
-1: Use `File.WriteAllText` (with encoding) or `File.WriteAllBytes` in place of this type of code.
280Z28
For strings it is slightly pointless as BinaryWriter uses 2 bytes per character as it's encoding Unicode. However this matches the question title so +1'd you
Chris S
+3  A: 

Definitely has the foul stench of homework to it.

Hint - Think why someone might want to do this. Just deleting the file and replacing with a file of 0s of the correct length might not be what you're after.

Martin Milan
+7  A: 

I'm not sure why you'd want to do this, but this will overwrite a file with data that is the same length but contains byte values of zero:

File.WriteAllBytes(filePath, new byte[new FileInfo(filePath).Length]);
Daniel Earwicker
+1 for a succinct one liner!
Jesse C. Slicer
What happens if the file is enormous?
Will Vousden
@Zakalwe - it needs virtual memory equal to the file size. So for very large files, compile in 64-bit mode! :)
Daniel Earwicker
@Earwicker: Or use buffering!
Will Vousden
@Zakalwe - hmm, that was intended as a joke. Serious answer: if you know that every bit in a terabyte is zero, why would you need to actually store them in the first place?
Daniel Earwicker
@Earwicker: Yeah your comment had me scratching my head a bit! I don't know, though; just sticking the question's spec :P
Will Vousden
+1  A: 

When you say write all bits to a file I'll assume you mean bits as in nyble, bit, byte. That's just writing an integer to a file. You can't have a 4 bit file as far as I know so the smallest denomination will be a byte.

You probably don't want to be responsible for serializing yourself, so your easiest option would be to use the BinaryReader and BinaryWriter classes, and then manipulate the bits inside your C#.

The BinaryWriter class uses a 4 byte integer as minimum however. For example

writer.Write( 1 ); // 01  
writer.Write( 10 ); // 0a  
writer.Write( 100 ); // 64  
writer.Write( 1000 ); // 3e8  
writer.Write( 10000 ); // 2710  
//writer.Write( 123456789 ); // 75BCD15

is written to file as

01 00 00 00 0a 00 00 00 64 00 00 00 e8 03 00 00 10 27 00 00 15 cd 5b 07
Chris S
+/-0: For clarity, the choice should always be `File.ReadAllText`/`ReadAllBytes`/`ReadAllLines` and the `WriteAll*` complements, and only use the `BinaryReader`/`BinaryWriter` if you have a more complicated case that prevents the simple versions.
280Z28
Unless you want a smaller filesize and don't want to write your own formatter. For example 1000 is 4 bytes in a text file, 2 with the `BinaryWriter`. `BinaryWriter` is also used heavily by binary serialization.
Chris S