0

我需要检查文件是否为.doc,.ppt.pdf任何其他文件。我写了以下代码:

bool CheckFile(string path)
{
    char * sig;
    sig = new char[8];
    ifstream myfile;
    myfile.open(path.c_str(), ios::in | ios::binary);
    if (myfile.fail())
    {
        MessageBox(0,"File Not Opened","ERROR",MB_OK);
        break;
    }
    myfile.read(sig,8);

    //docx, pptx, xlsx
    if ((sig[0] == (0x50))&&(sig[1] == (0x4B))&&(sig[2] == (0x03))&&(sig[3] == (0x04))&&(sig[4] == (0x14))&&(sig[5] == (0x00))&&(sig[6] == (0x06))&&(sig[7] == (0x00)))
    {
        return true;
    }

    //doc, ppt, xls
    if ((sig[0] == (0xD0))&&(sig[1] == (0xCF))&&(sig[2] == (0x11))&&(sig[3] == (0xE0))&&(sig[4] == (0xA1))&&(sig[5] == (0xB1))&&(sig[6] == (0x1A))&&(sig[7] == (0xE1)))
    {
        return true;
    }

    //pdf
    if ((sig[0] == (0x25))&&(sig[1] == (0x50))&&(sig[2] == (0x44))&&(sig[3] == (0x46)))
    {
        return true;
    }
    delete sig;
    myfile.close();
    return false;
}

我在网上查了一下,发现我们可以比较签名,即 MS office 文件的前 8 个字节和 case 的前 4 个字节PDFs。在上面的代码中,我也在做同样的事情。在和 Office 2007 格式的情况下返回,包括CheckFile()and但在和的情况下返回。a文件的控制台输出为:TRUEPDFs.docx.pptxFALSE.doc.ppt.doc

FFFFFFD0
FFFFFFCF
11
FFFFFFE0
FFFFFFA1
FFFFFFB1
1A
FFFFFFE1

其中每一行对应于 sig 中 char 的十六进制。请注意,最后一个字节与文件的签名相同.doc。我不知道为什么这些额外的东西FFFFFF会出现在这里。可能是什么问题呢 ??

4

1 回答 1

1

As for the problem with FFFFFFFF, you might notice that the last byte of those numbers are larger than 0x7f which means that they, for a signed byte is negative. So you are using a signed char and the compiler sign-extends it when you print the values.

You should change to unsigned char (or even better, the standard type uint8_t).

于 2013-05-29T13:49:30.047 回答