첫 번째 scanf()가 사용되고 Y로 대답하면 두 번째 scanf()는 "No option selected, Exiting ..."로 바로 건너 뜁니다. 이 메시지는 keyfile이 sourcefile보다 크고 마지막 scanf가 제대로 작동하면 나타납니다. 그래서 내가 여기서 잃어버린거야, 뭐가 잘못 됐니? (코드는 멋지게 컴파일되므로 자유롭게 시도하십시오.)C - scanf misbehaving
편집 : 적어도 이유를 게시하는 것이 downvoters에게 도움이 될 것입니다. 나는 아주 좋은 프로그래머가 아니며 여기서 배우려고하고있다.
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int main(int argc, char **argv)
{
struct stat statbuf;
struct stat keybuf;
int key;
int data;
int output;
int count;
char ans;
FILE * keyfile;
FILE * sourcefile;
FILE * destfile;
if(argc<4)
{
printf("OTP-Bunny 1.0\n");
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
return (0);
}
/* Check number of arguments. */
if(argc>4)
{
printf("Too many arguments.\n");
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
return(1);
}
/* Check if sourcefile can be opened. */
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");
perror("Error");
return (1);
}
/* Get size of sourcefile */
fstat(fileno(sourcefile), &statbuf);
/* Check if keyfile can be opened. */
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");
perror("Error");
return(1);
}
/* Get size of keyfile */
fstat(fileno(keyfile), &keybuf);
/* 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");
}
else
{
printf("No option selected. Exiting...\n");
return (1);
}
}
/* Check if destfile can be opened. */
if((keyfile = fopen(argv[2], "wb"))== NULL)
{
printf("Can't open output file.\n");
perror("Error");
return(1);
}
/* Open destfile. */
destfile=fopen(argv[2], "wb");
/* Encrypt/Decrypt and write to output file. */
while(count < (statbuf.st_size))
{
key=fgetc(keyfile);
data=fgetc(sourcefile);
output=(key^data);
fputc(output,destfile);
count++;
}
/* Close files. */
fclose(keyfile);
fclose(sourcefile);
fclose(destfile);
printf("Encryption/Decryption Complete.\n");
/* Delete keyfile option. */
printf("Do you wish to delete the keyfile? (Y/N)\n");
scanf("%c", &ans);
if(ans == 'y' || ans == 'Y')
{
if (remove(argv[3]) == 0)
{
printf("File deleted successfully.\n");
}
else
{
printf("Unable to delete the file.\n");
perror("Error");
return(1);
}
}
if(ans == 'n' || ans == 'N')
{
return(0);
}
else
{
printf("No option selected. Exiting...\n");
}
return(0);
}
오류 메시지에는 열 수없는 파일 이름이 포함되어야합니다 (예 : 사용자에게 호의). 에러 메시지는'stderr'에 쓰여지는 것이 아니라'stdout'에 쓰여 져야합니다. 오류보고 기능을 생각해보십시오. –