简体   繁体   English

C remove()函数不会在Windows环境中删除目标文件

[英]C remove() function doesn't delete target file in windows environment

I'm writing a program that asks the user for a file name, and creates it if it doesn't exist. 我正在编写一个程序,要求用户提供文件名,如果不存在则创建它。 At the end of the program, I want to check if the created program is empty, and if it is, delete it. 在程序末尾,我要检查创建的程序是否为空,如果为空,则将其删除。 Not deleting it and then running the program with that same file name messes up the way the input is detected. 不删除它,然后使用相同的文件名运行该程序会弄乱检测输入的方式。

I've tried using rewind() to go back to the beginning and then checking feof() to see if the beginning of the file was the EOF character, but that didn't work. 我试过使用rewind()返回到开头,然后检查feof()来查看文件的开头是否是EOF字符,但这没有用。

Then, I did some searching online, and found a method that used fseek() to go to the end of the file, and then checked with ftell() whether the end of the file was at position 0, but again this did not work. 然后,我在网上进行了一些搜索,找到了一种使用fseek()转到文件末尾的方法,然后使用ftell()检查文件末尾是否在位置0,但这再次不起作用。

I went back and did more poking around, and found that the problem might be because I hadn't used fclose() first, so I tried the previous two attempted solutions again, this time being sure to close the file before trying to delete it. 我回过头去做了更多的探索,发现问题可能是因为我没有先使用fclose() ,所以我再次尝试了前两个尝试的解决方案,这次确保在试图删除文件之前先关闭文件。 Still no dice. 仍然没有骰子。

I tried checking what errno was set to, and got 2: No such file or directory . 我尝试检查errno设置为什么,并得到2: No such file or directory This is patently false, since if that was the case, it would mean that I had accomplished my goal, and when I check the working directory, the file is still there. 这显然是错误的,因为如果是这种情况,那意味着我已经实现了我的目标,并且当我检查工作目录时,文件仍然存在。

I have absolutely no idea what to try next. 我完全不知道下一步该怎么做。 Can anyone point me in the right direction? 谁能指出我正确的方向?

Here are the ways I've tried to delete the file ( fp is the file pointer, and file is a char pointer with the name of the file that fp points to.) : 这是我尝试删除文件的方式( fp是文件指针,而file是具有fp指向的文件名的char指针。):

Attempt 1: 尝试1:

rewind(fp);
if(feof(fp)){
    remove(file);
}

Attempt 2: 尝试2:

fseek(fp, 1, SEEK_END);
long size = ftell(fp);
if(size == 0){
    remove(file);
}

Attempt 3: 尝试3:

fseek(fp, 1, SEEK_END);
long size = ftell(fp);
fclose(fp);
if(size == 0){
    remove(file);
}

Attempt 4: 尝试4:

rewind(fp);
int empty = 0;
if(feof(fp)){
    empty = 1;
}
fclose(fp);
if(empty == 1){
    remove(file);
}

UPDATE: Here's a couple MCVEs, one for each method. 更新:这是几个MCVE,每种方法一个。

#include <stdio.h>
#include <errno.h>

int main() {
    FILE *fp;
    char file[40];
    scanf(" %[^\n]s", file);
    fp = fopen(file, "r");
    if(fp == NULL){
        fp = fopen(file, "w");
    int result;
    rewind(fp);
    int empty = 0;
    if(feof(fp)){
        empty = 1;
    }
    fclose(fp);
    if(empty == 1){
        result = remove(file);
    }
    printf("%d\n", result);
    printf("%d\n", errno);
    return 0;
}

Version 2: 版本2:

#include <stdio.h>
#include <errno.h>

int main() {
    FILE *fp;
    char file[40];
    scanf(" %[^\n]s", file);
    fp = fopen(file, "r");
    if(fp == NULL){
        fp = fopen(file, "w");
    int result;
    fseek(fp, 1, SEEK_END);
    long size = ftell(fp);
    fclose(fp);
    if(size == 0){
        result = remove(file);
    }
    printf("%d\n", result);
    printf("%d\n", errno);
    return 0;
}

UPDATE 2: 更新2:

I just realized that when I was making the MCVEs, when I ran them, result was returning 0 , which should have meant that it was successful, but the file was still there in the directory. 我只是意识到,当我制作MCVE时,当我运行它们时, result返回0 ,这本来意味着它成功了,但是文件仍然在目录中。 I'm at a loss for words. 我不知所措。

代码没有到达remove语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM