c使用fstat问题比较文件大小

c使用fstat问题比较文件大小,c,file,filesize,stat,fstat,C,File,Filesize,Stat,Fstat,比较两种文件大小的代码似乎表现为keyfile>sourcefile。下面的代码中是否缺少某些内容?我可以更改哪些内容?我用于测试的两个文件是3kb的keyfile和14kb的sourcefile,它们应该激活下面提供的最后一个if语句 /* Get size of sourcefile. */ if((sourcefile = fopen(argv[1], "rb"))== NULL) { printf("Can't open source file.\n"); printf("Please

比较两种文件大小的代码似乎表现为
keyfile
>
sourcefile
。下面的代码中是否缺少某些内容?我可以更改哪些内容?我用于测试的两个文件是3kb的keyfile和14kb的sourcefile,它们应该激活下面提供的最后一个
if
语句

/* Get size of sourcefile. */
if((sourcefile = fopen(argv[1], "rb"))== NULL)
{
printf("Can't open source file.\n");
printf("Please enter a valid filename.\n");
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
return (1);
}

fflush(sourcefile);
fstat(fileno(sourcefile), &statbuf);
fclose(sourcefile);

/* Get size of keyfile. */
if((keyfile = fopen(argv[3], "rb"))== NULL)
{
printf("Can't open keyfile.\n");
printf("Please enter a valid filename.\n"); 
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
return(1);

fflush(keyfile);
fstat(fileno(keyfile), &keybuf);
fclose(keyfile);
}

/* Open necessary files. */
keyfile=fopen(argv[3], "rb");
sourcefile=fopen(argv[1], "rb");
destfile=fopen(argv[2], "wb");

/* Check if keyfile is the same size as, or bigger than the sourcefile */
if((keybuf.st_size) < (statbuf.st_size))
{
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((sourcefile=fopen(argv[1],“rb”))==NULL)
{
printf(“无法打开源文件。\n”);
printf(“请输入有效的文件名。\n”);
printf(“用法:OTP\n”);
申报表(1);
}
fflush(源文件);
fstat(fileno(sourcefile)和statbuf);
fclose(源文件);
/*获取密钥文件的大小*/
if((keyfile=fopen(argv[3],“rb”))==NULL)
{
printf(“无法打开密钥文件。\n”);
printf(“请输入有效的文件名。\n”);
printf(“用法:OTP\n”);
申报表(1);
fflush(keyfile);
fstat(fileno(keyfile)和keybuf);
fclose(keyfile);
}
/*打开必要的文件*/
keyfile=fopen(argv[3],“rb”);
sourcefile=fopen(argv[1],“rb”);
destfile=fopen(argv[2],“wb”);
/*检查keyfile是否与sourcefile大小相同或大于sourcefile*/
如果((键槽标准尺寸)<(标准尺寸))
{
printf(“源文件大于密钥文件。\n”);
printf(“这大大降低了加密强度。\n”);
printf(“您想继续吗?(是/否)\N”);
scanf(“%c”和“&ans”);
如果(ans=='n'| ans=='n')
{
申报表(1);
}
if(ans=='y'| | ans=='y')
{
printf(“正在进行加密/解密。\n”);
}
}   

这是因为如果无法打开密钥文件,您只能
fstat
密钥文件。把你的卷发撑起来。另外,作为表单的优化,不要打开文件,对其进行统计,关闭并重新打开。只是不要关上它继续

既然你问了

/* Get size of sourcefile. */
if((sourcefile = fopen(argv[1], "rb"))== NULL)
{
printf("Can't open source file.\n");
printf("Please enter a valid filename.\n");
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
return (1);
}

fflush(sourcefile);
//fstat(fileno(sourcefile), &statbuf);   // <-- this is not needed
//fclose(sourcefile);                    // <-- this is not needed

/* Get size of keyfile. */
if((keyfile = fopen(argv[3], "rb"))== NULL)
{
printf("Can't open keyfile.\n");
printf("Please enter a valid filename.\n"); 
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
return(1);
    }                                     // <-- this brace is new (well, moved) (1)

fflush(keyfile);
//fstat(fileno(keyfile), &keybuf);    // <-- not needed
//fclose(keyfile);                    // <-- not needed
//}                                     // <-- this brace has moved up 4 lines to (1)

/* Open necessary files. */
keyfile=fopen(argv[3], "rb");
sourcefile=fopen(argv[1], "rb");
destfile=fopen(argv[2], "wb");

/* Check if keyfile is the same size as, or bigger than the sourcefile */
if((keybuf.st_size) < (statbuf.st_size))
{
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((sourcefile=fopen(argv[1],“rb”))==NULL)
{
printf(“无法打开源文件。\n”);
printf(“请输入有效的文件名。\n”);
printf(“用法:OTP\n”);
申报表(1);
}
fflush(源文件);

//fstat(fileno(sourcefile),&statbuf);//你不需要打开你的文件两次…啊,是的,你的意思是在/*打开必要的文件*/?没有密钥文件和源文件也可以。接下来进行编辑。另外,您不检查是否可以打开
destfile
。如果无法写入指定的文件(权限错误等),则此操作将失败。@halex-OP询问它为什么会以相反的方式运行(即,他询问它为什么会以keyfile.size>statbuf.size的方式运行)。因为您实际上试图自己实现它,并且希望验证,所以请确定。请参阅我的最新答案。希望它读起来不错。另外,你不需要
fflush
it。你没有写任何东西。互联网上应该有更多像你这样的人哈哈。超级英雄(ish)=P