Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening long file names in Windows using fopen with C

Tags:

c

windows

I have trouble here using fopen to open files which have paths longer than the 260 characters supported by Windows natively.

I found out about the prefix \\\\?\\ which I need to put in front of the path to be able to handle the file.

My question is: Is this still valid in combination with fopen? I have still trouble to open the files, but I do not find information about it. My paths look like:

\\\\?\\C:\\Deposit\\Source\\Here_Comes_Now_A_List_Of_Many_Subdirs_And_A_Long_File_Name

I am not able to use the Windows API due to the requirement to write a cross-platform tool.

like image 537
Rick-Rainer Ludwig Avatar asked Oct 30 '25 16:10

Rick-Rainer Ludwig


1 Answers

Just to update with the current state: I'm just quoting https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#enable-long-paths-in-windows-10-version-1607-and-later:

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

And it is:

1. Having <longPathAware>true</longPathAware> in your application manifest (it was not default in my C++ Visual Studio project). My manifest file looks like this:

<assembly>
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings">
      <ws2:longPathAware>true</ws2:longPathAware>
    </windowsSettings>
  </application>
</assembly>

and I have added it into Project Options -> Manifest Tool -> Input and Output -> Additional manifest files in my C++ Visual Studio project.

2. Enabled long paths in Windows (this can be done through registry (in item Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled by setting to 1) or Microsoft says that in Local Group Policy (item Computer Configuration -> Administrative Templates -> System -> Filesystem -> Enable Win32 long paths) - but this does not work for me).

Having both the above configured, you can simply use FILE *f = fopen("../my-very-long-path/my-file.txt") without any limitations (e.g. relative dirs and / with \ replacement work).

like image 148
Jarek C Avatar answered Nov 02 '25 04:11

Jarek C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!