gpt4 book ai didi

c - 文件删除和重命名错误

转载 作者:太空宇宙 更新时间:2023-11-04 04:55:46 25 4
gpt4 key购买 nike

我正在学习 C,我正在尝试重命名和删除文件。这是我的代码:

    fclose(satis);
if(!remove("satis.txt"))
printf("file deleting error\n");
fclose(gecicisatis);
if(!rename("gecicisatis.txt", "satis.txt"))
printf("file rename error\n");

这是我的 .cpp 文件的标题:

#include<stdio.h>
#include<locale.h>
#include<string.h>
#include<windows.h>
#include<conio.h>

调试时没有任何错误。但它的输出是:

file deleting error
file rename error

如果你需要,这是我的全部代码:http://pastebin.com/kNY0y2xz (第 157 到 162 行)

这是我们的作业,所以我必须使用 txt 文件。我必须解决为什么文件删除和删除返回错误。你能帮我解决这个问题吗?

最佳答案

remove 成功时返回 0。所以你需要在 if 中反转你的条件。此外,每当您遇到错误情况时,最好调用函数 perror ,它将打印错误原因:

if(remove("satis.txt"))
perror("File deletion failed\n");

rename 函数也是如此。

由于您只提供了文件名而不是路径名,因此假设该文件存在于可执行文件启动的目录中(假设您没有更改进程的密码)。如果不是这种情况,则必须传递路径名,它可以是相对的也可以是绝对的。

关于c - 文件删除和重命名错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8575077/

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