gpt4 book ai didi

C - remove() 不会删除 char *

转载 作者:行者123 更新时间:2023-11-30 17:02:12 25 4
gpt4 key购买 nike

我试图用remove()删除一个文件,由于某种原因,当我给它一个char*中的路径时,它不起作用。这是我所拥有的:

#include<stdio.h>

int main(int argc, char *argv[]){

const char * toDie = "/home/User/Desktop/todie.txt";

int status = remove(toDie);

if( status != 0 ){
printf("Unable to delete the file\n");
}
}

当我运行时它工作正常

 int status = remove("/home/User/Desktop/todie.txt");

有人可以解释一下吗?

最佳答案

您应该首先检查该文件是否存在,或者您是否提供了正确的路径或该文件的正确名称。

试试这个:

#include<stdio.h>
#include<stdlib.h>

int main(void){

const char * toDie = "/home/User/Desktop/todie.txt";

FILE *check = fopen(toDie, "r");

if(!check){
printf("There is no file with that name\n");

}

int status = remove(toDie);

if( status != 0 ){
printf("Unable to delete the file\n");
exit(1);
}else{
printf("File removed successfully");
}
}

也许你注意到我改变了:

int main(int argc, char *argv[]){}

与:

int main(void){}

因为,如果您不带参数运行该程序,则不需要:

(int argc, char *argv[])

关于C - remove() 不会删除 char *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36668002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com