So i have an assignment to write a code that reads its own .exe file, and output only the ASCII characters contained within. Below, is what i have so far...
My professor told me i just have to get it to read the file char by char, and output only the ASCII chars in it, but i have no idea how to do that, please help.
first of all you have a couple of tasks that appear to be confusing the issue...
I can't ell you that reading the file from an exe is going to be able to be read into a binary but I will give you a sample from my book that reads data from a file, and will try and explain the little bit that I understand.
here's the sample code from the book and I have to give credit to the Author Mr. Tony Gaddis...
// This program reads data from a file.
#include<iostream>
#include<fstream>
#include<cctype>
#include<string>
using namespace std;
int main()
{
ifstream inputFile; //File stream object
string name; //To hold the name from the file
// Open a file named names.txt
inputFile.open("names.txt");
// Read the name from the file.
while (inputFile >> name) //here the file is read into the variable name and stored
{
//Display the name
cout << name << endl; //The stored data is printed out here
}
//Close the file
inputFile.close();
return 0;
}
Additionally I would add that in order to display the strings and to identify which ones are alpha, numerals, etc...then you have to use the various isalpha(), isspace() and to get each char from a string, you have to process it one element at a time with a for loop...
That said: you might be able to adopt some of this to help you...
/* isalpha example from learncplusplus.com */
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
int i= 0;
char str[] = "C++";
while (str[i])
{
if (isalpha(str[i]))
{
cout << "Character is alphabetic: \t" << str[i] << endl;
}
else
cout << "Character is not alphabetic: \t" << str[i] << endl;
i++;
}
return 0;
}
//This is a test program for counting the letters of an alphabet array of chars.
#include <iostream>
using namespace std;
int main()
{
const int alpha_Array = 26;
char letter[alpha_Array]; /* = { 'A','B','C','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };
//this is where the formatting goes for how you want to design your output to the console
for (int count = 0; count < alpha_Array; count++)
cout << letter[count] << "\n";
/* This cout: "cout << letter[count] << "\n";" --> outputs this:
A
B
C
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Press any key to continue . . .*/
So now I would guess you might be able to see some of the process.
If you are simply printing out the ascii you must be sure to get the value and not the reference address! I can aslo add that if you want to get the alpha in one array and the numbers in another array a separate function for each one and then nest an if statement to process them.
I hope this isn't too debillitating, and confusion becomes the master. If so, I hope you get additional help and God Bless!
A file is a sequence of bytes. In C and C++, a char is a byte. So all you need to do is read the file one character at a time to get one byte at a time.
I will give my opinion that your professor doesn't actually want you to print ASCII characters, but he wants you to print printable characters, which you can identify using one of the following C Library functions (#include <cctype>):
isprint() -- includes the space character isgraph() -- does not include the space character
To open your own executable, use the first command-line argument.
When you start a new VS C++ project for your classes, make sure to specify an empty, console application. The project should not have any stdafx.h or TCHAR stuff in it.