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

lines 60 and 61: you don't need both of those lines -- delete one of the two.

line 67: you are mixing up files opened in text mode and files opened in binary mode. read() method is for binary mode, not text mode. If it is really a binary file then you need to add ios::binary flag in the open statement like this: file_in.open(file_in_name, ios::binary); line 76: you need to use the value returned by gcount() instead of just hard coding the number 16 because the last read may or may not be 16, and anything between the number of bytes actually read and 16 will just be garbage.

Also, this (starting at line 132) won't do what you want it to.

while(!opFile.eof())
        {
            getline(opFile,templine);
            cout << "\nTEMPLINE: " << templine;
            //if ((offset = line.find(search, 0)) != string::npos) {
            //    cout << "found '" << search << "' @ offset " << offset << endl;
            //}
        }

The while(!eof) idiom is nearly always a bad idea. The reason for this is that the file doesn't know it has reached its end until you actually call a function, such as getline(), which reads data from the file. (I'm not going to expand on this here; see the FAQ for details.) In this case, my experimentation indicates that getline() sets its string to the empty string even if it fails to read any input, which is lucky, and means that you're probably safe in writing this: -- but in similar constructs in other languages it would be a bad idea.

Also, why not just use the simple alternative that avoid the while(!eof) construct?

while(getline(opFile,templine))
        {
            cout << "\nTEMPLINE: " << templine;
            //if ((offset = line.find(search, 0)) != string::npos) {
            //    cout << "found '" << search << "' @ offset " << offset << endl;
            //}
        }

That always works, and is less typing besides . . . .

commented: Yes :) +25

Thank you very much, AncientDragon and dwks.

The code still won't display the contents of temp.txt so I decided to rewrite it The problem was solved when I made a new function for the 'compare' part and called it after line 20 (from the code I posted above). I took note of the solutions you gave too.

Again, thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.