Fill in the bolded section below. using System; using System.Collections.Generic
ID: 3548339 • Letter: F
Question
Fill in the bolded section below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
class wk13asgn
{
// Instance variables for opening and reading the file
static string file_name;
static BinaryReader in_stream;
static FileStream fs;
// Instance variable used to find the end of file
static bool end_of_file = false;
// Instance variables used by getbit()
static byte current_bit_position = 0;
static byte current_byte;
static void get_file_name()
/*********************/
/*** GET FILE NAME ***/
/*********************/
{
Console.Write("Enter File Name: ");
file_name = Console.ReadLine();
}
static void open_input_file()
/*****************/
/*** OPEN FILE ***/
/*****************/
{
try
{
fs = new FileStream(file_name, FileMode.Open, FileAccess.Read);
in_stream = new BinaryReader(fs);
}
catch (Exception)
{
Console.WriteLine("Can't open {0}", file_name);
Environment.Exit(0);
}
}
static bool no_more_bytes(BinaryReader rb)
/**********************/
/*** NO MORE BYTES? ***/
/**********************/
{
return (rb.PeekChar() == -1);
}
static byte read_a_byte(BinaryReader rb)
/********************************************************************************/
/*** Function to read a single byte from the input stream and set end_of_file ***/
/********************************************************************************/
{
byte abyte;
if (!no_more_bytes(rb))
abyte = rb.ReadByte();
else
{
abyte = 0;
end_of_file = true;
}
return abyte;
}
static byte getbit()
/**********************************************************/
/*** Function to get a single BIT from the input stream ***/
/**********************************************************/
{
byte mask;
//Your job is to fill in this function so it returns
//either a zero or a one for each bit in the file.
}
static void process()
/********************/
/*** PROCESS FILE ***/
/********************/
{
byte bit;
int counter = 0;
while (!end_of_file)
{
bit = getbit();
if (!end_of_file)
{
Console.Write("{0}", bit);
counter++;
//We only separate bytes to make the output easier to read.
//Your program should treat each bit individually.
if ((counter % 8) == 0) Console.Write(" ");
}
}
Console.WriteLine();
}
static void Main(string[] args)
{
if (args.Length == 0)
get_file_name();
else
file_name = args[0];
open_input_file();
process();
in_stream.Close();
fs.Close();
}
}
Explanation / Answer
static byte getbit() /**********************************************************/ /*** Function to get a single BIT from the input stream ***/ /**********************************************************/ { byte mask;//here we use fill in this function so it returns //either a zero or a one for each bit in the file.
mask = (byte)(128 >> current_bit_position); if (current_bit_position == 0) current_byte = read_a_byte(in_stream); byte bit = (byte)((current_byte & mask) >> (7 - current_bit_position)); current_bit_position++; if (current_bit_position == 8) current_bit_position = 0; return bit; }
I tested the program on a file containing the characters AZby which produced the following output:
01000001 01011010 01100010 01111001
These, of course, are the binary representations of 65, 90, 98 and 121 which are the ASCII codes of the characters in the file.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.