{"record":{"id":"36d68d74ccb732f2","repo":"Yalantis/uCrop","slug":"cimg-fclose-error-code-d-returned-during-file","errorCode":null,"errorMessage":"cimg::fclose(): Error code %d returned during file closing.","messagePattern":"cimg::fclose\\(\\): Error code (.+?) returned during file closing\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":7584,"sourceCode":"#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;\n    }\n\n    //! Version of 'fseek()' that supports >=64bits offsets everywhere (for Windows).\n    inline int fseek(FILE *stream, cimg_long offset, int origin) {\n#if defined(WIN64) || defined(_WIN64) || defined(__WIN64__)\n      return _fseeki64(stream,(__int64)offset,origin);\n#else\n      return std::fseek(stream,offset,origin);\n#endif\n    }\n\n    //! Version of 'ftell()' that supports >=64bits offsets everywhere (for Windows).\n    inline cimg_long ftell(FILE *stream) {\n#if defined(WIN64) || defined(_WIN64) || defined(__WIN64__)\n      return (cimg_long)_ftelli64(stream);\n#else","sourceCodeStart":7566,"sourceCodeEnd":7602,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L7566-L7602","documentation":"cimg::fclose() wraps std::fclose() and warns when the close call returns a nonzero error code, meaning buffered data may not have been flushed and the file was not closed properly. It is a non-fatal diagnostic; the return code is still propagated to the caller.","triggerScenarios":"Calling cimg::fclose() on a real FILE* (not stdin/stdout/null) when the underlying close/flush fails — e.g. disk full, medium removed, or the stream is already in an error state after a prior failed write.","commonSituations":"Writing large images to a full disk or removable drive; network/USB storage dropping mid-write; ignoring earlier write errors so fclose also fails at flush time.","solutions":["Check the return value of cimg::fclose() and treat nonzero as a save failure (the file may be corrupt)","Check free disk space and medium health on the target volume","Check for prior write errors (ferror) before closing, and retry the save","On Windows, ensure no other process holds the file open"],"exampleFix":"// before\nimg.save(\"out.bmp\");\ncimg::fclose(file);\n// after\nif (cimg::fclose(file) != 0) {\n  std::fprintf(stderr, \"file close failed - output may be incomplete\\n\");\n}","handlingStrategy":"try-catch","validationCode":"// before closing\nif (file && !std::ferror(file)) {\n  int r = cimg::fclose(file);\n  if (r != 0) fprintf(stderr, \"close failed (%d), output may be corrupt\\n\", r);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always check cimg::fclose's return value after saving","Check disk free space before large writes","Detect earlier write failures with ferror() so failed saves are retried"],"tags":["cimg","file-io","fclose"],"backgroundTag":"file-close-failed","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}