I am trying to create and write to a file using one boolean bit at a time.
-
// I AM TRYING TO WRITE AND CREATE A FILE, USING ONLY ONE BIT AT A TIME.
// I AM WRITING THE FILE, BUT NOTHING GOES IN.
// ALSO I GETTING ERRORS FOR BEING OUTSIDE OF THE ARRAY.
using System;
using System.IO;
using System.Collections;
namespace Applica
{
static class Program
{
static void Main(string[] args)
{
bool tif = true;
byte[] buffer = BitConverter.GetBytes(tif);
FileInfo ap = new FileInfo("tempii.txt");
string filePath = ap.FullName;
string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));
using (Stream output = File.OpenWrite(destinationPath))
{
int bits = 32;
while (bits > 0)
{
for (int i = 1; i < 33; i++)//4 random bytes
{
if (tif == true)
{
tif = false;
goto A;
}
if (tif == false) tif = true;
A:;
output.Write(buffer, 0, bits);
}
}
}
}
}
} -
// I AM TRYING TO WRITE AND CREATE A FILE, USING ONLY ONE BIT AT A TIME.
// I AM WRITING THE FILE, BUT NOTHING GOES IN.
// ALSO I GETTING ERRORS FOR BEING OUTSIDE OF THE ARRAY.
using System;
using System.IO;
using System.Collections;
namespace Applica
{
static class Program
{
static void Main(string[] args)
{
bool tif = true;
byte[] buffer = BitConverter.GetBytes(tif);
FileInfo ap = new FileInfo("tempii.txt");
string filePath = ap.FullName;
string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));
using (Stream output = File.OpenWrite(destinationPath))
{
int bits = 32;
while (bits > 0)
{
for (int i = 1; i < 33; i++)//4 random bytes
{
if (tif == true)
{
tif = false;
goto A;
}
if (tif == false) tif = true;
A:;
output.Write(buffer, 0, bits);
}
}
}
}
}
}I bet you're not.
-
// I AM TRYING TO WRITE AND CREATE A FILE, USING ONLY ONE BIT AT A TIME.
// I AM WRITING THE FILE, BUT NOTHING GOES IN.
// ALSO I GETTING ERRORS FOR BEING OUTSIDE OF THE ARRAY.
using System;
using System.IO;
using System.Collections;
namespace Applica
{
static class Program
{
static void Main(string[] args)
{
bool tif = true;
byte[] buffer = BitConverter.GetBytes(tif);
FileInfo ap = new FileInfo("tempii.txt");
string filePath = ap.FullName;
string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));
using (Stream output = File.OpenWrite(destinationPath))
{
int bits = 32;
while (bits > 0)
{
for (int i = 1; i < 33; i++)//4 random bytes
{
if (tif == true)
{
tif = false;
goto A;
}
if (tif == false) tif = true;
A:;
output.Write(buffer, 0, bits);
}
}
}
}
}
}Oh dear :-D - The smallest unit of "writable data" is a byte. - Drop that
goto
(and please don't use it ever again, for anything). Instead: Toggling a boolean variable is as easy as this:tif = !tif;
- Doesn't help a lot here though because the change will not reflect in
buffer
. After you createbuffer
it keeps its value forever. - You're using the third parameter of theStream.Write
-method wrongly - at least judging from your described intent. It's supposed to be the amount of bytes frombuffer
, starting at position 0, that should be written into the file stream. Butbuffer
is only 1 byte long andbits
says there should be 32 bytes. That's where you are "outside of the array bounds". - Yourwhile
-loop will run forever becausebits
will be > 0 forever. You should start using the debugger. Place a breakpoint at the start of your method (first line or opening brace) by placing the cursor there and pressing F9. Then start your program in debug mode by pressing F5. Then step over the execution line-by-line by pressing F10*
while you inspect the values of your variables by hovering with the mouse cursor over it. Compare the values you expect with their actual values and when there's a difference try to find out, why.*
: Take a look at further ways of stepping through your code while debugging: In the Debug-Menu and/or the Debugging-Toolbar (and the Debugger-documentation).If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
// I AM TRYING TO WRITE AND CREATE A FILE, USING ONLY ONE BIT AT A TIME.
// I AM WRITING THE FILE, BUT NOTHING GOES IN.
// ALSO I GETTING ERRORS FOR BEING OUTSIDE OF THE ARRAY.
using System;
using System.IO;
using System.Collections;
namespace Applica
{
static class Program
{
static void Main(string[] args)
{
bool tif = true;
byte[] buffer = BitConverter.GetBytes(tif);
FileInfo ap = new FileInfo("tempii.txt");
string filePath = ap.FullName;
string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));
using (Stream output = File.OpenWrite(destinationPath))
{
int bits = 32;
while (bits > 0)
{
for (int i = 1; i < 33; i++)//4 random bytes
{
if (tif == true)
{
tif = false;
goto A;
}
if (tif == false) tif = true;
A:;
output.Write(buffer, 0, bits);
}
}
}
}
}
} -
// I AM TRYING TO WRITE AND CREATE A FILE, USING ONLY ONE BIT AT A TIME.
// I AM WRITING THE FILE, BUT NOTHING GOES IN.
// ALSO I GETTING ERRORS FOR BEING OUTSIDE OF THE ARRAY.
using System;
using System.IO;
using System.Collections;
namespace Applica
{
static class Program
{
static void Main(string[] args)
{
bool tif = true;
byte[] buffer = BitConverter.GetBytes(tif);
FileInfo ap = new FileInfo("tempii.txt");
string filePath = ap.FullName;
string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));
using (Stream output = File.OpenWrite(destinationPath))
{
int bits = 32;
while (bits > 0)
{
for (int i = 1; i < 33; i++)//4 random bytes
{
if (tif == true)
{
tif = false;
goto A;
}
if (tif == false) tif = true;
A:;
output.Write(buffer, 0, bits);
}
}
}
}
}
}Sorry, may I ask, why you try to do so? Do you try to solve HDD performance issue or save flash memory read-write cycles? Thanks!