我正在研究处理覆盖目录中文件的程序的一部分。覆盖文件很容易......但显然不会提示用户是否希望覆盖文件。例如:如果我想覆盖两个文件(fileOne.txt、fileTwo.txt),我需要提示用户两次(每个文件一次)。在我当前的提示下,如果我提示用户覆盖 fileOne.txt,fileOne.txt 和 fileTwo.txt 都将被覆盖。如果有两个以上的文件要覆盖,它将只覆盖两个连续的文件。我相信这与点击“Enter”有关,但我不知道......
if(((int)getHeader.deleted - 48) == 0) {
if(access(getHeader.file_name, F_OK) != -1) { /* File exists in directory, check for overwrite */
printf("%s already exists. Would you like to overwrite it? (y/n)\n", getHeader.file_name);
scanf("%c", buffer);
while(!validResponse) {
if(buffer[0] == 'y' || buffer[0] == 'Y') {
validResponse = 1;
printf("DO SOMETHING - Files will be overwritten\n");
} else if(buffer[0] == 'n' || buffer[0] == 'N') {
validResponse = 1;
printf("DO SOMETHING - File will be skipped\n");
} else {
printf("Invalid response... Would you like to overwrite %s? (y/n)\n", getHeader.file_name);
scanf("%c", buffer);
} /* End if else */
} /* End while */
} /* End if */
} /* End if */