c сравнение размеров файлов с использованием fstat isue
Мой код, сравнивающий 2 размера файла, ведет себя так, как будто 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");
}
}
1 ответ
Решение
Это потому что ты только 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");
}
}