The program gets the hexdump of a file and saves it in a text file (temp.txt). The program should then get the contents of temp.txt and two other text files (virus1sig.txt & virus2sig.txt) and display them.
The problem is only the contents of virus1sig.txt & virus2sig.txt are being displayed.
Thank you!
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main(long argc, char *argv[]);
void handle(char file_in_name[]);
int main(long argc, char *argv[])
{
char file_in_name[80];
int i;
if(argc<=1)
{
cout << " Please enter the name of a file to be scanned: \n";
cin.getline(file_in_name, sizeof(file_in_name));
handle(file_in_name);
}
else
{
for(i=1; i<argc; ++i)
{
handle(argv[i]);
}
}
return 0;
}
void handle(char file_in_name[])
{
long int addr;
unsigned char buffer[20];
long int cnt;
long int cnt2;
ifstream file_in;
long n;
// Open file
file_in.open(file_in_name);
if(!file_in)
{
cout << "\n";
cout << "HANDLE - Fatal error!\n";
cout << " Cannot open " << file_in_name << "\n";
return;
}
// create .txt file for signature
string filename;
filename=file_in_name;
filename="temp.txt";
ofstream file;
file.open(filename.c_str());
while(1)
{
file_in.read((char *) buffer, 16);
cnt=file_in.gcount();
if(cnt<=0)
{
break;
}
// hex
cnt2=0;
for(n=0; n<16; n++)
{
cnt2 = cnt2+1;
if(cnt2<=cnt)
{
cout << hex << setw(2) << setfill('0') << (int)buffer[n];
file << hex << setw(2) << setfill('0') << (int)buffer[n];
//file << "";
}
else
{
cout << " ";
}
cout << "";
}
cout << setfill(' ');
}
// Close the file.
file_in.close();
// COMPARE
string templine;
int vsig1offset;
string getvsig1;
string getvsig2;
// GET TEMP.TXT CONTENTS
ifstream opFile("temp.txt");
if(opFile.is_open())
{
while(!opFile.eof())
{
getline(opFile,templine);
cout << "\nTEMPLINE: " << templine;
//if ((offset = line.find(search, 0)) != string::npos) {
// cout << "found '" << search << "' @ offset " << offset << endl;
//}
}
opFile.close();
}
else
cout << "Unable to open this file." << endl;
//VIRUS SIG1 COMPARE
ifstream vsig1("virus1sig.txt");
if(vsig1.is_open())
{
while (!vsig1.eof())
{
getline(vsig1,getvsig1);
cout << "\nGETVSIG1: " << getvsig1;
//if((vsig1offset = getvsig1.find(templine, 0)) != string::npos) {
// cout << getvsig1 << endl;
// cout << "found '" << templine << "' @ offset " << vsig1offset << endl;
}
vsig1.close();
}
ifstream vsig2("virus2.txt");
if(vsig2.is_open())
{
while (!vsig2.eof())
{
getline(vsig2,getvsig2);
cout << "\nGETVSIG2: " << getvsig2;
//if((vsig1offset = getvsig2.find(templine, 0)) != string::npos) {
// cout << getvsig1 << endl;
// cout << "found '" << templine << "' @ offset " << vsig1offset << endl;
}
vsig2.close();
}
}