Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I am trying to create and write to a file using one boolean bit at a time.

I am trying to create and write to a file using one boolean bit at a time.

Scheduled Pinned Locked Moved C#
data-structureslounge
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    computerpublic
    wrote on last edited by
    #1

    // 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);
    }
    }
    }
    }
    }
    }

    P S L _ 4 Replies Last reply
    0
    • C computerpublic

      // 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);
      }
      }
      }
      }
      }
      }

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      I bet you're not.

      1 Reply Last reply
      0
      • C computerpublic

        // 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);
        }
        }
        }
        }
        }
        }

        S Offline
        S Offline
        Sascha Lefevre
        wrote on last edited by
        #3

        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 create buffer it keeps its value forever. - You're using the third parameter of the Stream.Write-method wrongly - at least judging from your described intent. It's supposed to be the amount of bytes from buffer, starting at position 0, that should be written into the file stream. But buffer is only 1 byte long and bits says there should be 32 bytes. That's where you are "outside of the array bounds". - Your while-loop will run forever because bits 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

        1 Reply Last reply
        0
        • C computerpublic

          // 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);
          }
          }
          }
          }
          }
          }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Reminds me of when I got my first 8088. Now I use MemoryStream and BinaryWriter.

          "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

          1 Reply Last reply
          0
          • C computerpublic

            // 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);
            }
            }
            }
            }
            }
            }

            _ Offline
            _ Offline
            ________________
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups