我目前正在編寫一個簡單的一次性填充程序。我希望它在keyfile
小於source
文件時提醒用戶,但是當我運行它時,我的程序完全忽略它。C程序忽略語句是否包含大小
/* 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");
}
我懷疑問題在於我使用sizeof
,但我不知道任何其他的選擇嗎?任何關於如何改變我的代碼以使其工作的建議?
編輯:我曾嘗試添加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);
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);
}
和改變的,如果第一線陳述被忽略:
if((keybuf.st_size) < (statbuf.st_size))
爲什麼它仍然被程序忽略?
感謝那些花時間回答的人。
http://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c – Paulpro
由於決策是在編譯時進行的,顯示了變量的正確聲明' keyfile'和'sourcefile'可能無所謂,但爲了完整性,你應該這樣做。 –
如果您希望人們看到它,並且爲了便於閱讀,請考慮創建另一個問題。 – tomahh