我正在尝试从指定的文件夹中删除文件。我的deleteFile()
函数仅在其主文件夹上删除,而不会删除我需要/tmp
文件夹上。我尝试了与displayDIR()
函数相同的方法来更改目录,但我不知道如何使其工作。我使用cygwin作为编译器。
void deleteFile() {
int status;
char filetodelete[25];
printf("n t **Delete File**n");
displayDIR();
printf("ntChoose 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("ntUnable to delete the file");
perror("ntError");
}
}
void displayDIR() {
DIR *d;
struct dirent *dir;
d = opendir("C:/cygwin/tmp");
if (d) {
while ((dir = readdir(d)) != NULL)
printf("ttt%sn", dir->d_name);
closedir(d);
}
}
您需要
在参数中包含文件夹路径以remove()
:
char fullpath[40] = "C:/cygwin/tmp/";
strcat(fullpath, filetodelete);
status = remove(fullpath);