Yalantis/uCrop · error · CImgIOException

cimg::fopen(): Failed to open file '%s' with mode '%s'.

Error message

cimg::fopen(): Failed to open file '%s' with mode '%s'.

What it means

cimg::fopen() throws CImgIOException when the underlying std_fopen (or stdin/stdout handling) fails and returns 0, meaning the file could not actually be opened with the requested mode. Unlike error [20], the arguments are valid; the OS refused or failed the open (missing file, permissions, bad path, etc.).

Source

Thrown at ucrop/src/main/jni/CImg.h:7568

      if (!path)
        throw CImgArgumentException("cimg::fopen(): Specified file path is (null).");
      if (!mode)
        throw CImgArgumentException("cimg::fopen(): File '%s', specified mode is (null).",
                                    path);
      std::FILE *res = 0;
      if (*path=='-' && (!path[1] || path[1]=='.')) {
        res = (*mode=='r')?cimg::_stdin():cimg::_stdout();
#if cimg_OS==2
        if (*mode && mode[1]=='b') { // Force stdin/stdout to be in binary mode
#ifdef __BORLANDC__
          if (setmode(_fileno(res),0x8000)==-1) res = 0;
#else
          if (_setmode(_fileno(res),0x8000)==-1) res = 0;
#endif
        }
#endif
      } else res = cimg::std_fopen(path,mode);
      if (!res) throw CImgIOException("cimg::fopen(): Failed to open file '%s' with mode '%s'.",
                                      path,mode);
      return res;
    }

    //! Close a file.
    /**
       \param file File to close.
       \return \c 0 if file has been closed properly, something else otherwise.
       \note Same as <tt>std::fclose()</tt> but display a warning message if
       the file has not been closed properly.
    **/
    inline int fclose(std::FILE *file) {
      if (!file) { warn("cimg::fclose(): Specified file is (null)."); return 0; }
      if (file==cimg::_stdin(false) || file==cimg::_stdout(false)) return 0;
      const int errn = std::fclose(file);
      if (errn!=0) warn("cimg::fclose(): Error code %d returned during file closing.",
                        errn);
      return errn;

View on GitHub (pinned to f788b534b4)

Solutions

  1. Verify the file exists and the path is correct (use absolute paths in JNI code)
  2. Check file permissions and that the target directory is writable
  3. Check errno (access ENOENT/EACCES/EMFILE) to pinpoint the cause before retrying
  4. In "w" mode, confirm the directory exists and is not read-only

Example fix

// before
std::FILE *f = cimg::fopen("images/out.png", "wb"); // dir may not exist
// after
cimg::mkdir("images"); // ensure directory exists (or check access first)
std::FILE *f = cimg::fopen("images/out.png", "wb");
Defensive patterns

Strategy: validation

Validate before calling

struct stat st; if (stat(path, &st) != 0) { /* file missing or inaccessible */ }
if (*mode=='w' && access(dir, W_OK) != 0) { /* dir not writable */ }

Try / catch

try { f = cimg::fopen(path, mode); } catch (const cimg_library::CImgIOException &e) { log_error("open failed: %s (errno=%d)", e.what(), errno); }

Prevention

When it happens

Trigger: cimg::fopen(path, mode) where the OS open() fails: nonexistent file in "r" mode, no write permission in "w" mode, invalid path, too many open FDs, or device errors.

Common situations: Reading an image from a wrong relative path (working-directory mismatch); writing to a read-only directory or external storage on Android; file deleted between existence check and open; ulimit -n exhaustion.

Understand the failure class

Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/8612997883caf6c0. Report an issue: GitHub.