{"record":{"id":"023551978f76cfc5","repo":"Yalantis/uCrop","slug":"cimg-fwrite-only-lu-lu-elements-could-be-wri","errorCode":null,"errorMessage":"cimg::fwrite(): Only %lu/%lu elements could be written in file.","messagePattern":"cimg::fwrite\\(\\): Only %lu/%lu elements could be written in file\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":7935,"sourceCode":"       \\return Number of written elements.\n       \\note Similar to <tt>std::fwrite</tt> but may display warning messages if all elements could not be written.\n    **/\n    template<typename T>\n    inline size_t fwrite(const T *ptr, const size_t nmemb, std::FILE *stream) {\n      if (!ptr || !stream)\n        throw CImgArgumentException(\"cimg::fwrite(): Invalid writing request of %u %s%s from buffer %p to file %p.\",\n                                    nmemb,cimg::type<T>::string(),nmemb>1?\"s\":\"\",ptr,stream);\n      if (!nmemb) return 0;\n      const size_t wlimitT = 63*1024*1024, wlimit = wlimitT/sizeof(T);\n      size_t to_write = nmemb, al_write = 0, l_to_write = 0, l_al_write = 0;\n      do {\n        l_to_write = (to_write*sizeof(T))<wlimitT?to_write:wlimit;\n        l_al_write = std::fwrite((void*)(ptr + al_write),sizeof(T),l_to_write,stream);\n        al_write+=l_al_write;\n        to_write-=l_al_write;\n      } while (l_to_write==l_al_write && to_write>0);\n      if (to_write>0)\n        warn(\"cimg::fwrite(): Only %lu/%lu elements could be written in file.\",\n             (unsigned long)al_write,(unsigned long)nmemb);\n      return al_write;\n    }\n\n    //! Create an empty file.\n    /**\n       \\param file Input file (can be \\c 0 if \\c filename is set).\n       \\param filename Filename, as a C-string (can be \\c 0 if \\c file is set).\n    **/\n    inline void fempty(std::FILE *const file, const char *const filename) {\n      if (!file && !filename)\n        throw CImgArgumentException(\"cimg::fempty(): Specified filename is (null).\");\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"wb\");\n      if (!file) cimg::fclose(nfile);\n    }\n\n    // Try to guess format from an image file.\n    inline const char *ftype(std::FILE *const file, const char *const filename);","sourceCodeStart":7917,"sourceCodeEnd":7953,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L7917-L7953","documentation":"cimg::fwrite() writes nmemb elements of size sizeof(T) to a C stream in bounded chunks and emits a non-fatal warning when the stream accepted fewer elements than requested. The function returns the count actually written, so the warning indicates a short write (disk full, closed stream, quota exceeded) rather than throwing. CImg save routines use it when dumping pixel data.","triggerScenarios":"Calling CImg<T>::fwrite()/save routines when the output disk is full, the FILE* was closed or hit an error mid-write, the target is a pipe that broke, or a quota/filesystem limit stops the write partway.","commonSituations":"Saving images to a full disk or a small tmpfs, writing to a closed stdout/pipe (e.g. process at the other end exited), permission/quota issues on the output path.","solutions":["Check fwrite()'s return value equals the requested element count and treat a shortfall as a save failure","Free disk space or write to a different volume, then retry the save","Verify the output stream is still open/writable before saving (ferror(), for pipes check the reader)","Write to a temp file and atomically rename into place so partial writes never masquerade as a successful save"],"exampleFix":"// before\nimg.save(\"/mnt/data/out.bmp\"); // disk full -> partial file, only a warning\n// after\nif (!img.save(\"/mnt/data/out.bmp.tmp\")) throw std::runtime_error(\"save failed (short write?)\");\nstd::rename(\"/mnt/data/out.bmp.tmp\", \"/mnt/data/out.bmp\");","handlingStrategy":"validation","validationCode":"if (!std::FILE* f = std::fopen(out, \"wb\")) throw std::runtime_error(\"cannot open output\");\n// after saving:\nif (std::fflush(f) != 0 || std::ferror(f)) throw std::runtime_error(\"write failed\");","typeGuard":"bool stream_writable(std::FILE* f) { return f && !std::ferror(f); }","tryCatchPattern":null,"preventionTips":["Check fwrite/fflush return values; treat short writes as fatal save failures","Verify free disk space before writing large images","Write to temp file then rename for atomic saves","Avoid saving to pipes/streams you do not control"],"tags":["file-io","short-write","disk-full","cimg"],"backgroundTag":"file-write-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"}