I need help with an assignment that I was tasked with. My code for a binary file
ID: 3758531 • Letter: I
Question
I need help with an assignment that I was tasked with. My code for a binary file viewer to read byte by byte and output into hexadecimal followed by the ASCII value:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
#include <cstdlib>
void PressEnterToContinue();
using namespace std;
int main(int argc, char *argv[]){
int n = 0;
int r;
int x = 0, m = 0, a = 0;
ifstream file(argv[1]);
int size;
char *file_buffer ;
if((argc < 2) || (strcmp(argv[1], "/?") == 0)){
cout << "Binary file viewer ";
cout << "Usage: hw5[input filename] ";
}
file.open(argv[1], ios::binary);
if(!file.is_open())
{
cout<<"Please enter a valid file name. ";
cin >> argv[1];
}
else
{
n = 1;
}
while(n != 1);
file.seekg(0, ios::end);
size = file.tellg();
file_buffer = new char[size];
file.seekg(0, ios::beg);
file.read(file_buffer, size);
do{
for(int g = 0; g <= 20; g++)
{
for(x; x < 16 + a; x++)
{
r = file_buffer[x];
cout << hex << setw(2) << uppercase << r << " ";
}
cout << " ";
for(m; m < 16 + a; m++)
{
if(iscntrl(file_buffer[m]))
cout << ".";
else
cout << file_buffer[m];
}
a+=16;
cout << endl;
}
PressEnterToContinue();
}while(x<=size);
file.close();
}
void PressEnterToContinue( )
{
cout << "Press ENTER to continue...";
cin.ignore();
}
The output should look like something below:
to create a binary file viewer. This program wil read in For this homework you will have to create a binary file viewer. This program will read in a file byte for byte and print the hexadecimal values and the ASCII representation to the console output, 16 bytes at a time. After reading 20 lines of 16 bytes, the user should be prompted with a message "Press ENTER to continue". If the end of file has not been detected, the program will start reading in the next 20 lines of 16 bytes (or less if the EOF file has been detected). A binary file viewer will view all the bytes in the file, including whitespace, and will print a '' whenever the byte is considered a control character. You should use C++ code for opening and reading files, displaying messages to the console output and reading in from the console output (printf, fprintf, fscanf, fopen, and fclose functions should NOT be used) nsole output (printf, fprintf, fscanf, fopen, and fclose An example output of the program would look like Visual Studio Command Prompt (2010) ents HW5 Solut ion>hw5 ex3.cpp #include . # include .using na mespace std:.. vo D: Users t im Documents Dropbox Fall 2012 EGRE-246 Assignm 63 6C 61 6D 3E A 23 69 6E 63 6C 75 64 65 20 3C 69 6F 60 61 6E 69 70 3E A A 75 73 69 6E 67 20 6E 61 60 657370 61 63 65 20 73 74 64 3B A A 76 6F 69 64 20 6D 61 69 6E 28 76 6F 69 64 29 A 7B A 62 6F 6F 6C 20 61 20 3D 20 74 72 75 65 3BA 64 6F 75 62 6C 65 20 62 20 3D 20 31 30 31 38 2E 30 30 3B A 9 69 6E 74 20 63 20 30 20 32 35 35 3B A 9 69 6E 74 20 64 20 3D 20 31 2C 20 65 20 3D 20 20 31 3B A 9 66 6C 6F 61 74 20 70 69 SF 66 20 3D 20 33 2E 31 34 31 35 39 32 36 35 33 35 39 3B A 9 64 6F 75 62 6C 65 20 70 69 5F 64 20 3D 20 33 2E 31 34 31 35 39 32 36 35 33 35 39 3B A A A 9 2F 2F 20 50 72 69 6E 74 20 62 6F 6F 6C 65 61 6E 20 61 73 20 74 72 75 65 20 6F 72 20 66 61 6C 73 65 A 9 63 6F 75 74 20 3C 3C 20 22 61 20 3D 20 22 20 3C 3C 20 61 20 3C 3C 20 65 6E 64 6C 3B A 9 63 6F 75 74 20 3C 3C 20 62 6F 6F 6C 61 6C 70 68 61 3B A9 63 6F 75 74 20 3C 3C 20 22 61 20 3D 20 22 20 3C 3C 20 61 20 3C 3C 25 5 .float f = 3.1415926 3.14159265359 Print bo olean as true or false..cout K ndl:..coutExplanation / Answer
Modified Program:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
#include <cstdlib>
void PressEnterToContinue();
using namespace std;
int main(int argc, char *argv[]){
int n = 0;
int r;
int x = 0, m = 0, a = 0;
ifstream file;
int size;
char *file_buffer ;
file.open(argv[1], std::ifstream::binary);
if((argc < 2) || (strcmp(argv[1], "/?") == 0))
{
cout << "Binary file viewer ";
cout << "Usage: hw5[input filename] ";
}
if(!file.is_open())
{
cout<<"Please enter a valid file name. ";
cin >> argv[1];
}
else
{
n = 1;
}
while(n != 1);
file.seekg(0, ios::end);
size = file.tellg();
file_buffer = new char[size];
file.seekg(0, ios::beg);
file.read(file_buffer, size);
do{
for(int g = 0; g < 20; g++)
{
for(x; x < 16 + a; x++)
{
if(x>size)
break;
r = file_buffer[x];
cout<<hex << setw(2) << uppercase << r << " ";
}
cout << " ";
for(m; m < 16 + a; m++)
{
if(m>size)
break;
if(iscntrl(file_buffer[m]))
cout << ".";
else
cout << file_buffer[m];
}
a+=16;
cout << endl;
}
PressEnterToContinue();
}while(x<=size);
file.close();
}
void PressEnterToContinue( )
{
cout << "Press ENTER to continue...";
cin.ignore();
}
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.