0

I use fopen to open some files given their absolute path. It opens most of the files but some of them don't.

I thought it was because of the file itself but when I manually shorten the name of the folder which contains it there is no problem with opening it.

Is there a way to deal with this without having to rename that folder?

EDIT

Just mention I'm not working in Windows, but Linux.

Peter B.
  • 21
  • 4
  • Possible duplicate of [Opening long file names in Windows using fopen with C](https://stackoverflow.com/questions/6829691/opening-long-file-names-in-windows-using-fopen-with-c) – José Feb 16 '19 at 17:04
  • 2
    Please show your code and the file path in question. Without a [mcve] we can only guess at the problem. – John Kugelman Feb 16 '19 at 17:05
  • How long are your absolute path names? 256 bytes? 1024 bytes? Longer? There are limits like `NAME_MAX` for individual file components (between a pair of slashes, or after the last slash, or before the first slash) and `PATH_MAX` for the sum of the lengths of all the components in a file name. There are lower bounds on those limits, too: `_POSIX_NAME_MAX`, `_XOPEN_NAME_MAX`, `_POSIX_PATH_MAX`, `_XOPEN_PATH_MAX` all in [``](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html). – Jonathan Leffler Feb 16 '19 at 20:09
  • If you really have path names that are too long, you may have to use [`openat()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/openat.html) to open a directory part way along the full name, and maybe then use that in a subsequent call to open another directory (and repeat as needed) until you can use it to open the file relative to the last directory you `openat()`'d. You should be able to traverse multiple levels of the directory hierarchy on each `openat()` call. Frankly, though, you should be reconsidering why the names are so long that they cause problems. – Jonathan Leffler Feb 16 '19 at 20:14

1 Answers1

0

In Windows, you should use _wfopen(). Look at this Microsoft reference of _wfopen().

In a example the can be found at this other SO answer, it teaches you to do a cross-platform file opening:

#ifdef WIN32
    myFile = _wfopen( ... );
#else
    myFile = fopen( ... );
#endif
José
  • 354
  • 5
  • 18