gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:07:31 24 4
gpt4 key购买 nike

我的代码比较 2 个文件大小,其行为似乎类似于 keyfile > sourcefile。下面的代码是否缺少某些内容以及我可以更改什么?我用于测试的两个文件是 3kb key 文件和 14kb 源文件,它们应该激活下面提供的最后一个 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");
}
}

最佳答案

这是因为如果 key 文件无法打开,您只能fstat key 文件。将大括号向上移动。另外,作为表单的优化,不要打开文件,统计它,关闭它并重新打开它。只是不要关闭它并继续。

既然你问了

/* 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");
}
}

关于c 使用 fstat 问题比较文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12866343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com