C++中的类型转换static_cast

// Compose.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define Byte50M  52428800
#define HeadEnd  0xFF

typedef struct sonfile
{
    unsigned char sum;  //总共分成多少个文件
    unsigned char order;//子文件在原文件中的顺序
    char* source;   //原文件名
};

int sfheadsize(char* filename);
sonfile readsfhead(char* filename);

int main(int argc,char* argv[])
{
    if (argc < 1)
        exit(-2);
    for (int i = 1; i < argc; i++)
    {
        printf("head size:%d file:%s\n", sfheadsize(argv[i]), argv[i]);
        sonfile sf = readsfhead(argv[i]);
        printf("sonfile.sum:%d\nsonfile.order:%d\nsonfile.source:%s\n", sf.sum, sf.order, sf.source);
    }
    return 0;
}

int sfheadsize(char* filename)
{
    FILE* fp = fopen(filename, "r");
    char temp = '\0';
    int count = 0;
    do
    {
        fread(&temp, 1, 1, fp);
        ++count;
        if (temp == EOF)
            break;
    } while (temp != HeadEnd);
    fclose(fp);
    return count;
}

sonfile readsfhead(char* filename)
{
    sonfile sf;
    memset(&sf, 0, sizeof(sf));
    FILE* fp = fopen(filename, "r");
    char temp = '\0';
    fread(&temp, 1, 1, fp);
    sf.sum = static_cast(temp);
    fread(&temp, 1, 1, fp);
    sf.order = static_cast(temp);
    stringstream sstr;
    string srcfname;
    do
    {
        fread(&temp, 1, 1, fp);
        sstr << temp;
        sstr >> srcfname;
#ifdef _DEBUG
        printf("0x%02h ", temp);
#endif //_DEBUG
        if (temp == EOF)
            break;
    } while (temp != HeadEnd);
#ifdef _DEBUG
    cout << srcfname << endl;
#endif //_DEBUG
    fclose(fp);
    return sf;
}

 

你可能感兴趣的:(C/C++)