为什么fread_s在这个C代码中不起作用?

时间:2012-11-23 21:50:45

标签: c file pointers fread

虽然所有参数都是正确的,但它返回读取的项的正确值,函数fread_s不会在“bytes”中记录任何内容,它是空的。当我交换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;
}

编辑:解决了

2 个答案:

答案 0 :(得分:2)

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

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

因此,当您尝试从七字节文件中读取一百万个单字节对象时,您将获得其中七个,并且返回代码将反映这一点。但是,如果您尝试读取一百万字节的对象,则根本不会得到任何内容,返回代码将反映出来。

我强调返回值的原因是你可以获得比你想要的更少的对象(例如,如果你要求1000个单字节对象,文件中只剩下900个),所以盲目地写预期的输出文件的数量是禁止的。您需要检查返回代码并采取相应措施。

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

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

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

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

答案 1 :(得分:2)

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

您需要bytes,而不是&bytes,因为bytes中的地址是您的缓冲区所在的位置。