zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++中的类型转换static_cast

C++ 类型转换 static cast
2023-09-11 14:14:48 时间
// Compose.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <vld.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
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<unsigned char>(temp);
    fread(&temp, 1, 1, fp);
    sf.order = static_cast<unsigned char>(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;
}