0

尽管所有参数都是正确的,但它返回读取的项目的正确值,函数 fread_s 没有以“字节”为单位的任何内容,它是空的。当我交换 10485760 和 1 时它也是空的。有谁知道是什么导致了这个问题?文件完全没有问题。

float EncryptBig(CRYPTIN* handle)
{
    int i, index = 0;
    float calc;
    char* bytes;

    i = (handle->size - handle->huidig);
    if ((i-10485760) < 0)
    {
        bytes = (char*)malloc(i);
        if (bytes == NULL)
        {
            fcloseall();
            free(handle);
            return 100.0f;
        }

        fread_s(&bytes, i, 1, i, handle->bestand); // Here and down below
        fclose(handle->bestand);

        for (index = 0; index < i; index++)
        {
            __asm
            {
                mov         eax, dword ptr [bytes]  
                add         eax, dword ptr [index]  
                mov         cl, byte ptr [eax]
                xor         cl, 101
                xor         cl, 53
                not         cl
                mov         byte ptr [eax], cl 
                mov         eax, dword ptr [index]  
                add         eax, 1  
                mov         dword ptr [index], eax
            }
        }

        fwrite(bytes, 1, i, handle->nieuwbstnd);
        fclose(handle->nieuwbstnd);
        free(handle);
        free(bytes);

        return 100.0f;
    }

    if (handle->huidig == 0)
    {
        fseek(handle->bestand, 0, SEEK_SET);
        fseek(handle->nieuwbstnd, 0, SEEK_SET);
    }

    bytes = (char*)malloc(10485760);
    if (bytes == NULL)
    {
        fcloseall();
        free(handle);
        return 100.0f;
    }
    fread_s(bytes, 10485760, 10485760, 1, handle->bestand); // Here

    for (index = 0; index < 10485760; index++)
    {
        __asm
        {
            mov         eax, dword ptr [bytes]  
            add         eax, dword ptr [index]  
            mov         cl, byte ptr [eax]
            xor         cl, 101
            xor         cl, 53
            not         cl
            mov         byte ptr [eax], cl 
            mov         eax, dword ptr [index]  
            add         eax, 1  
            mov         dword ptr [index], eax
        }
    }

    fwrite(bytes, 1, 10485760, handle->bestand);
    free(bytes);
    handle->huidig += 10485760;
    handle->positie += 10485760;
    fseek(handle->bestand, handle->huidig, SEEK_SET);
    fseek(handle->nieuwbstnd, handle->positie, SEEK_SET);
    calc = (float)handle->huidig;
    calc /= (float)handle->size;
    calc *= 100.0f;

    if (calc >= 100.0)
    {
        fclose(handle->bestand);
        fclose(handle->nieuwbstnd);
        free(handle);
    }

    return calc;
}

编辑:解决

4

2 回答 2

1
bytes = (char*)malloc(i);
// etc.
fread_s(&bytes, i, 1, i, handle->bestand); // Here and down below

你想要bytes在这里,而不是&bytes因为地址bytes是你的缓冲区所在的地方。

于 2012-11-23T22:13:01.090 回答
1

不确定这是否是您的具体问题,但的代码有问题。

当您进行fread调用时,您指定要读取的“对象”数量和对象大小,然后fread将读取那么多对象。但它会读取这些对象的确切数量

因此,当您尝试从一个 7 字节文件中读取一百万个 1 字节对象时,您将获得其中的 7 个,并且返回码将反映这一点。但是,如果您尝试读取一百万字节的对象,您将一无所获,返回码将反映这一点。

我强调返回值的原因是您返回的对象可能比您想要的少(例如,如果您要求 1000 个单字节对象,而文件中只剩下 900 个),所以盲目地将预期数量写入输出文件是禁忌。您需要检查返回码并采取行动。

此外,正如评论者指出的那样,您似乎在您的一个输出语句中写回输入文件:

fwrite (bytes, 1, 10485760, handle->bestand);

这不太可能结束:-)它可能应该是:

fwrite (bytes, 1, 10485760, handle->nieuwbstnd);
于 2012-11-23T21:57:10.250 回答