简体   繁体   中英

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.

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.

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. Still no dice.

I tried checking what errno was set to, and got 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.) :

Attempt 1:

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

Attempt 2:

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

Attempt 3:

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

Attempt 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.

#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:

#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:

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. I'm at a loss for words.

代码没有到达remove语句。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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