{"record":{"id":"8612997883caf6c0","repo":"Yalantis/uCrop","slug":"cimg-fopen-failed-to-open-file-s-with-mode","errorCode":null,"errorMessage":"cimg::fopen(): Failed to open file '%s' with mode '%s'.","messagePattern":"cimg::fopen\\(\\): Failed to open file '(.+?)' with mode '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":7568,"sourceCode":"      if (!path)\n        throw CImgArgumentException(\"cimg::fopen(): Specified file path is (null).\");\n      if (!mode)\n        throw CImgArgumentException(\"cimg::fopen(): File '%s', specified mode is (null).\",\n                                    path);\n      std::FILE *res = 0;\n      if (*path=='-' && (!path[1] || path[1]=='.')) {\n        res = (*mode=='r')?cimg::_stdin():cimg::_stdout();\n#if cimg_OS==2\n        if (*mode && mode[1]=='b') { // Force stdin/stdout to be in binary mode\n#ifdef __BORLANDC__\n          if (setmode(_fileno(res),0x8000)==-1) res = 0;\n#else\n          if (_setmode(_fileno(res),0x8000)==-1) res = 0;\n#endif\n        }\n#endif\n      } else res = cimg::std_fopen(path,mode);\n      if (!res) throw CImgIOException(\"cimg::fopen(): Failed to open file '%s' with mode '%s'.\",\n                                      path,mode);\n      return res;\n    }\n\n    //! Close a file.\n    /**\n       \\param file File to close.\n       \\return \\c 0 if file has been closed properly, something else otherwise.\n       \\note Same as <tt>std::fclose()</tt> but display a warning message if\n       the file has not been closed properly.\n    **/\n    inline int fclose(std::FILE *file) {\n      if (!file) { warn(\"cimg::fclose(): Specified file is (null).\"); return 0; }\n      if (file==cimg::_stdin(false) || file==cimg::_stdout(false)) return 0;\n      const int errn = std::fclose(file);\n      if (errn!=0) warn(\"cimg::fclose(): Error code %d returned during file closing.\",\n                        errn);\n      return errn;","sourceCodeStart":7550,"sourceCodeEnd":7586,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L7550-L7586","documentation":"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.).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the file exists and the path is correct (use absolute paths in JNI code)","Check file permissions and that the target directory is writable","Check errno (access ENOENT/EACCES/EMFILE) to pinpoint the cause before retrying","In \"w\" mode, confirm the directory exists and is not read-only"],"exampleFix":"// before\nstd::FILE *f = cimg::fopen(\"images/out.png\", \"wb\"); // dir may not exist\n// after\ncimg::mkdir(\"images\"); // ensure directory exists (or check access first)\nstd::FILE *f = cimg::fopen(\"images/out.png\", \"wb\");","handlingStrategy":"validation","validationCode":"struct stat st; if (stat(path, &st) != 0) { /* file missing or inaccessible */ }\nif (*mode=='w' && access(dir, W_OK) != 0) { /* dir not writable */ }","typeGuard":null,"tryCatchPattern":"try { f = cimg::fopen(path, mode); } catch (const cimg_library::CImgIOException &e) { log_error(\"open failed: %s (errno=%d)\", e.what(), errno); }","preventionTips":["Use absolute paths in native/JNI code; the working directory differs from the app's assumption","Check errno right after a caught CImgIOException to distinguish ENOENT vs EACCES","Pre-create target directories before opening files for writing","Watch open-FD limits (ulimit -n) in loops that open many files"],"tags":["cimg","file-io","file-open","io-exception"],"backgroundTag":"file-open-failed","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}