Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C程序忽略包含sizeof的if语句_C_File_If Statement_Sizeof - Fatal编程技术网

C程序忽略包含sizeof的if语句

C程序忽略包含sizeof的if语句,c,file,if-statement,sizeof,C,File,If Statement,Sizeof,我目前正在编写一个简单的一次性pad程序。我希望它在密钥文件小于源文件时警告用户,但当我运行它时,我的程序会忽略它 /* Check if keyfile is the same size as, or bigger than the sourcefile */ if(sizeof keyfile < sizeof sourcefile) { printf("Source file is larger than keyfile.\n"); printf("This significantl

我目前正在编写一个简单的一次性pad程序。我希望它在密钥文件小于源文件时警告用户,但当我运行它时,我的程序会忽略它

/* Check if keyfile is the same size as, or bigger than the sourcefile */
if(sizeof keyfile < sizeof sourcefile)
{
printf("Source file is larger than keyfile.\n");
printf("This significantly reduces cryptographic strength.\n");
printf("Do you wish to continue? (Y/N)\n");
scanf("%c", &ans);
if(ans == 'n' || ans == 'N')
    {
    return (1);
    }
if(ans == 'y' || ans == 'Y')
    {
    printf("Proceeding with Encryption/Decryption.\n");
    }
并更改了被忽略的if语句的第一行:

if((keybuf.st_size) < (statbuf.st_size))
为什么程序仍然忽略它?

sizeof是一个编译时运算符,它产生操作数数据类型的大小(以字节为单位)。你确定这就是你想在这里做的吗?我非常怀疑,因为您似乎想要比较文件大小。fopen、fseek to end、ftell来获取文件的大小,或者操作系统的API为您提供的任何更舒适的函数。

sizeof不返回文件的大小。sizeof用于检查类型在内存中占用的字节数


如果您想了解有关获取文件大小的更多信息,请参阅post。

sizeof是一个一元运算符,用于计算数据类型的大小。您不能使用它来查找/比较文本文件的大小。

您需要使用它来获取文件的大小。

因为决定是在编译时做出的,所以显示变量keyfile和sourcefile的正确声明可能无关紧要,但为了完整性,你应该这样做。如果你想让人们看到它,并且为了可读性,请考虑创建另一个问题。当然,如何获得文件大小取决于平台…这就是为什么C标准没有涵盖它的原因。对于POSIX ish系统,stat是正确的。对于其他平台,答案因平台而异。@DevSolar-True如果是Windows。但是Windows确实支持POSIX,stat就是POSIX。只是使它更容易移植到UNIX以及其他一些操作系统
if((keybuf.st_size) < (statbuf.st_size))