Click here to Skip to main content
15,795,398 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
<pre>// 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);
                    }
                }
            }
        }
    }
}





What I have tried:

I am trying to write and create a file, using only one boolean bit at a time. I cant seem to get the boolean bits into the byte array that makes the file.
Posted
Updated 28-Nov-17 19:04pm
Comments
Patrice T 28-Nov-17 19:30pm    
Exact error message and position !
computerpublic 29-Nov-17 4:41am    
//I have modified the code as you suggested, and I am still getting the same error. Please see below.

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 bytes = 4;
while (bytes > 0)
{
for (int i = 1; i < 33; i++)//4 random bytes
{
if (tif == true)
{
tif = false;
}
else if (tif == false)
{
tif = true;
}
output.Write(buffer, 0, bytes);
}
}
}
}
}
}
Patrice T 29-Nov-17 7:26am    
Better style does not imply no error or correction.
Do you understand that your loop write 32 times the same thing to file? or at least try to write.

Use your debugger.

You will be receiving the following error message;
Error: An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Offset and length were out of bounds for the array or the count is greater that the number of elements from the index to the end of the source collection

The error will be caused by the line;
C#
output.Write(buffer, 0, bits);

As per the MSDN documentation - Stream.Write Method (Byte[], Int32, Int32) (System.IO)[^] - the Stream.Write method accepts an array of bytes (buffer), a position to start writing from (position 0 in your case) & the number of bytes to write (32 in your case).
The Byte array you have passed has a length of 1 bytes at all times, hence you cannot write 32 bytes...

Additionally, goto statements are poor programming in most people's opinion. Typically you would use it to break out of a deeply nested loop & in most cases you would probably just exit to another method

Kind Regards
 
Share this answer
 
This loop
C#
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);
}

is poor programming, a better style is
C#
for (int i = 1; i < 33; i++)//4 random bytes
{
    if (tif == true)
    {
        tif = false;
    }
    elseif (tif == false)
    {
        tif = true;
    }
    output.Write(buffer, 0, bits);
}

and can be simplified as
C#
for (int i = 1; i < 33; i++)//4 random bytes
{
    output.Write(buffer, 0, bits);
}


There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

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