我正在尝试从特定文件夹中删除文件。我的deleteFile()
函数只删除其主文件夹,而不是/tmp
文件夹,这是我需要的。我尝试了与displayDIR()
函数相同的方法来更改目录,但我无法弄清楚如何使其工作。我使用cygwin作为编译器。
void deleteFile() {
int status;
char filetodelete[25];
printf("\n \t **Delete File**\n");
displayDIR();
printf("\n\tChoose the name of the file to delete:\t");
scanf("%s", filetodelete);
status = remove(filetodelete);
if( status == 0 )
printf("%s file deleted successfully.\n", filetodelete);
else {
printf("\n\tUnable to delete the file");
perror("\n\tError");
}
}
void displayDIR() {
DIR *d;
struct dirent *dir;
d = opendir("C:/cygwin/tmp");
if (d) {
while ((dir = readdir(d)) != NULL)
printf("\t\t\t%s\n", dir->d_name);
closedir(d);
}
}
答案 0 :(得分:1)
您需要在remove()
的参数中包含文件夹路径:
char fullpath[40] = "C:/cygwin/tmp/";
strcat(fullpath, filetodelete);
status = remove(fullpath);