无法删除C文件夹中的文件
Can't delete file within folder in C
我正在终端中制作一个 txt 编辑器,它的功能之一是编辑特定行。
为此,
我正在创建一个新的临时 txt 文件,
删除 old/original 一个
并将临时的重命名为原来的。
代码如下:
FileLineEdit(char filename[20], int line, char newline[1000]){
FILE * fp;
FILE * fptmp;
char buffer[1000];
int count;
int ret;
fp = fopen(filename, "r");
fptmp = fopen("tmp/replace.txt", "w");
if (fp == NULL || fptmp == NULL)
{
printf("\nErro!\n");
exit(1);
}
count = 0;
while ((fgets(buffer, 1000, fp)) != NULL)
{
count++;
if (count == line)
fputs(newline, fptmp);
else
fputs(buffer, fptmp);
}
fclose(fp);
fclose(fptmp);
//strcat(fullpath, filename);
//printf("%s", fullpath);
ret = remove(filename);
if(ret == 0) {
printf("File deleted successfully");
} else {
printf("Error: unable to delete the file");
}
rename("tmp/replace.txt", "tmp/a.txt");
getch();
}
输出不断:
Error: unable to delete the file
顺便说一句,一旦我在“tmp/”文件夹外尝试它,它就可以正常工作
我正在终端中制作一个 txt 编辑器,它的功能之一是编辑特定行。 为此,
我正在创建一个新的临时 txt 文件, 删除 old/original 一个 并将临时的重命名为原来的。
代码如下:
FileLineEdit(char filename[20], int line, char newline[1000]){
FILE * fp;
FILE * fptmp;
char buffer[1000];
int count;
int ret;
fp = fopen(filename, "r");
fptmp = fopen("tmp/replace.txt", "w");
if (fp == NULL || fptmp == NULL)
{
printf("\nErro!\n");
exit(1);
}
count = 0;
while ((fgets(buffer, 1000, fp)) != NULL)
{
count++;
if (count == line)
fputs(newline, fptmp);
else
fputs(buffer, fptmp);
}
fclose(fp);
fclose(fptmp);
//strcat(fullpath, filename);
//printf("%s", fullpath);
ret = remove(filename);
if(ret == 0) {
printf("File deleted successfully");
} else {
printf("Error: unable to delete the file");
}
rename("tmp/replace.txt", "tmp/a.txt");
getch();
}
输出不断:
Error: unable to delete the file
顺便说一句,一旦我在“tmp/”文件夹外尝试它,它就可以正常工作