ERROR_PATH_NOT_FOUND vs ERROR_FILE_NOT_FOUND, what is the difference?
Asked Answered
P

3

6

I usually get ERROR_FILE_NOT_FOUND when I try to open a file that's not there, but now fopen() fails and GetLastError() returns ERROR_PATH_NOT_FOUND.

So what is the difference between ERROR_FILE_NOT_FOUND and ERROR_PATH_NOT_FOUND?

Plop answered 7/3, 2012 at 10:1 Comment(0)
C
14

In WinError.h, ERROR_FILE_NOT_FOUND has the descriptive text "The system cannot find the file specified." and ERROR_PATH_NOT_FOUND has the descriptive text "The system cannot find the path specified."

This doesn't particularly clarify matters.

Usually, however, "file not found" refers to the case where the file itself cannot be found and "path not found" refers to the case where a component of the path (one of the directory names specified) cannot be found.

Clearly answered 7/3, 2012 at 10:5 Comment(1)
Thanks. Turns out it was because I was %appdata% inside the path. It seems fopen doesn't accept envvars.Plop
S
8

Actually, the canonical meaning could be deduced from the names of the error codes.

The specific meaning, as it is with all "generic" error codes, highly depends on the implementation of the function that is said to "produce" this error. For an even worse example in this respect, consider the error ERROR_INVALID_DATA - only the documentation of the function could tell what should be ment by it.

This brings us to the point that fopen does not even (officially) return or set those error codes. fopen is part of the CRT library and is thus documented to use the error reporting mechanism thereof: errno.

Looking at the implementation of fopen in the CRT source code, you can see (ultimately, it is quite a callstack of internal helper functions), that fopen in the end calls (not surprisingly) the CreateFile Win32 API. It then carefully maps the errors potentially returned by CreateFile to errno-like errors (using the internal _dosmaperr() function, if you still want to follow in the CRT sources). This maps both ERROR_FILE_NOT_FOUND and ERROR_PATH_NOT_FOUND to the errno ENOENT.

So in the context of fopen the distinction is best considered meaningless as it is an implementation detail of fopen.

Sherysherye answered 7/3, 2012 at 10:26 Comment(1)
+1 for featuring the relation between fopen and CreateFileRoadbed
S
3

ERROR_PATH_NOT_FOUND means, that the container directory does not exist. ERROR_FILE_NOT_FOUND means, the container directory exists, and it does not contain the named file.

Sanders answered 7/3, 2012 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.