Advertisement

C++ std remove

Started by January 04, 2022 08:40 AM
4 comments, last by MikeCyber 2 years, 11 months ago

Hi

struct stat attrib;

int nowdate = time(0);

stat(InFileLo.c_str(), &attrib);

int moddate = attrib.st_mtime;

if ((nowdate - moddate) >= 160000 ){

remove(InFileLo.c_str());

why does the above not remove the file? I have the same remove code line in other places in my project where it works. Debug runs through so it's maybe stat related?

Many thanks

As far as I know, Windows File Time is different than whatever time returns. You have to convert one into another to get the exact time you want

Advertisement

Time is not the problem as it enters the if condition. Thanks

Depending on the OS and/or filesystem, you may not be allowed to remove the file.

Did you check the result value of ‘remove’ ? It's generally good practice to do so if possible.

EDIT: https://en.cppreference.com/w/cpp/io/c/remove

Deletes the file identified by character string pointed to by fname.

If the file is currently open by the current or another process, the behavior of this function is implementation-defined (in particular, POSIX systems unlink the file name, although the file system space is not reclaimed even if this was the last hardlink to the file until the last running process closes the file, Windows does not allow the file to be deleted)

Emphasis is mine.

Yes, I forgot to close the file before remove. Solved thanks.

This topic is closed to new replies.

Advertisement