2

I want to know whether fopen failed in C due to long path or file not exist

#include<stdio.h>
#include <errno.h>
extern int errno ;
int main(){
FILE *p;
int errnum;
p=fopen("C:\\Users\\kkm\\testtttttttttttttttttttttttttttttttttttttt\\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\\ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc\\b.txt","w");
//p=fopen("C:\\Users\\kkm\\testtttttttttttttttttttttttttttttttttttttt\\xxxx.txt","r");
if (p == NULL) {

      errnum = errno;
      fprintf(stderr, "Value of errno: %d\n", errno);
      perror("Error printed by perror");
      fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
   } else {

      fclose (p);
   }
return 0;
}

The first fopen has long path whereas the second one is short but file doesn't exist. For both these case output is similar:

Value of errno: 2
Error printed by perror: No such file or directory
Error opening file: No such file or directory

Is there a way to know the difference?

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Karthik K M
  • 619
  • 2
  • 9
  • 24
  • Does that **folder path** exist? `fopen` requires the folders to exist and be available. *"When you construct paths for fopen, make sure that drives, paths, or network shares will be available in the execution environment. "* – Weather Vane Mar 27 '20 at 13:19
  • The same error is reported by MS for "path does not exist" as for "path is too long". It's easy for you to check if the path is too long before you call `fopen`. (Similar to "how much memory did I allocate?" A: the amount you told it to allocate.) – Weather Vane Mar 27 '20 at 13:41
  • Yes folder exist. I think checking file size and then doing fopen wouldn't be great. Best would be what error fopen return and then processing accordingly. – Karthik K M Mar 27 '20 at 13:53

1 Answers1

2

Call a routine using the extended-length path syntax; prefixing the path with \\?\.

To specify an extended-length path, use the "\\?\" prefix. For example, "\\?\D:\very long path".

In your case, the path would be

"\\\\?\\C:\\Users\\kkm\\testtttttttttttttttttttttttttttttttttttttt\\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\\ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc\\b.txt"

The same link goes on to describe a new feature in Windows 10, Version 1607

Starting in Windows 10, version 1607, MAX_PATH limitations have been removed from common Win32 file and directory functions. However, you must opt-in to the new behavior.

To enable the new long path behavior, both of the following conditions must be met:

  • The registry key HKLM\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled (Type: REG_DWORD) must exist and be set to 1. ...
  • The application manifest must also include the longPathAware element. ...
Ðаn
  • 10,934
  • 11
  • 59
  • 95
  • Hi Dan Thanks for the extended length path solution. Is there a way to know the error There is a enum ENAMETOOLONG, are you aware of this? Can this be used here? – Karthik K M Mar 27 '20 at 13:54