Scroll Down to See All
abortabsacosasctimeasctime_rasinassertatanatan2atexitatofatoiatolbsearchbtowccalloccatclosecatgetscatopenceilclearerrclockcoscoshctimectime_rdifftimediverferfcexitexpfabsfclosefdopenfeofferrorfflushfgetcfgetposfgetsfgetwcfgetwsfopenfprintffputcfputwsfreadfreefreopenfrexpfscanffseekfsetposftellfwidefwprintffwscanfgetcgetchargetenvgetwcgetwchargmtimegmtime_rhypotisalnumisalphaisasciiisblankiscntrlisdigitisgraphislowerisprintispunctisspaceisupperiswalnumiswalphaiswblankiswcntrliswctypeiswdigitiswgraphiswloweriswprintiswpunctiswspaceiswupperiswxdigitisxdigitj0j1jnlabsldexpldivlocaleconvlocaltimelocaltime_rloglog10longjmpmallocmblenmbrlenmbrtowcmbsinitmbsrtowcsmbstowcsmbtowcmemchrmemcmpmemcpymemmovememsetmktimemodfnextafternextafterlnexttowardnexttowardlnl_langinfoperrorpowprintfputcputcharputenvputsputwcputwcharqsortquantexpd32quantexpd64quantexpd128quantized32quantized64quantized128samequantumd32raiserandrand_rreallocregcompregerrorregexecregfreeremoverenamerewindscanfsetbufsetjmpsetlocalesetvbufsignalsinsinhsnprintfsprintfsqrtsrandsscanfstrcasecmpstrcatstrchrstrcmpstrcollstrcpystrcspnstrerrorstrfmonstrftimestrlenstrncasecmpstrncatstrncmpstrncpystrpbrkstrptimestrrchrstrspnstrstrstrtodstrtod32strtod64strtod128strtofstrtokstrtok_rstrtolstrtoldstrtoulstrxfrmswprintfswscanfsystemtantanhtimetime64tmpfiletmpnamtoasciitolowertouppertowctranstowlowertowupperungetcungetwcva_argva_copyva_endva_startvfprintfvfscanfvfwprintfvfwscanfvprintfvscanfvsprintfvsnprintfvsscanfvswprintfvswscanfvwprintfvwscanfwcrtombwcscatwcschrwcscmpwcscollwcscpywcscpywcsftimewcslenwcsncatwcsncmpwcsncpywcspbrkwcsptimewcsrchrwcsrtombswcsspnwcsstrwcstodwcstod32wcstod64wcstod128wcstofwcstokwcstolwcstoldwcstombswcstoulwcsxfrmwctobwctombwctranswctypewcwidthwmemchrwmemcmpwmemcpywmemmovewmemsetwprintfwscanfy0y1yn



Function Details : fwrite

size_tfwrite(const void * ptr,size_t size,size_t nmemb,FILE * stream) ;

Return Type : size_t

Platform-specific unsigned type for array indices and memory sizes.
Read about return values of fwrite function .

1st Parameter Type : const void *

A pointer to a constant memory location of unspecified type. The pointed-to data cannot be modified through this pointer.

1st Parameter

Pointer to the buffer containing the data to be written.
Must point to valid memory block;

2nd Parameter Type : size_t

Platform-specific unsigned type for array indices and memory sizes.

2nd Parameter

The size of each element to be written, in bytes

3rd Parameter Type : size_t

Platform-specific unsigned type for array indices and memory sizes.

3rd Parameter

The number of elements to write

4th Parameter Type : FILE *

Pointer to FILE structure for file operations

4th Parameter

Pointer to the file stream where data will be written
It must be opened with write permissions ("w", "a", "r+", etc.)

Read more about parameters of fwrite in parameters section
The fwritefunction in C language writes a specified number of fixed-size data elements from memory to a file stream, returning the count of successfully written elements.
fwrite is essential for binary file operations and bulk data writing in C programming language, primarily used when dealing with structured data like arrays or structures that need to be written without any formatting or transformation. It is special function because it preserves the exact binary representation of data and offers optimized performance for large blocks through buffering. The function is commonly used with fread for binary I/O operations, ferror for error checking, and typically requires fopen with binary mode ("wb", "ab", "rb+") for proper binary handling.

To Summarize the Workflow:
  1. 1.
    Function writes blocks of data from memory to specified file stream
  2. 2.
    Takes data pointer to write from, specifies element size and count to control how much is written
  3. 3.
    Handles binary data safely, writing exact bytes without transformation
  4. 4.
    Returns total number of full elements successfully written
  5. 5.
    Can result in partial writes - less elements than requested written
  6. 6.
    Stream position automatically advances by number of bytes written
  7. 7.
    Uses error indicator for failure detection, works with error checking functions

Success

Failure

Yes

No

fwrite(const void *ptr, size_t size, size_t count, FILE *stream)

Write operation

Return count All elements written

Partial write?

Return number of elements written

Return 0 Set error indicator




Read more about return type and value offwrite function in return section.
The fwritefunction takes 4 parameters:
  • const void * `ptr`: Pointer to the buffer containing the data to be written.
    Must point to valid memory block;
  • size_t `size`: The size of each element to be written, in bytes
  • size_t `nmemb`: The number of elements to write
  • FILE * `stream`: Pointer to the file stream where data will be written
    It must be opened with write permissions ("w", "a", "r+", etc.)
Special cases:
● size = 0 or count = 0: no write occurs, returns 0;
● NULL ptr: undefined behavior;
● Invalid stream: returns 0, sets error indicator;
The fwrite function return value :
  • Returns the number of elements successfully written, which may be less than count if a write error occurs;
  • If size or count is zero, fwrite returns zero and the stream state remains unchanged;
  • Return value cases:
    count: All elements written successfully
    0: No elements written, error indicator set
    n < count: Partial write occurred
    Check ferror to confirm if partial write was due to error or not


Full success

Complete failure

Partial success

fwrite(ptr, size, count, stream)

Write attempt

Return count All elements written

Return 0 Set error indicator

Return n < count Partial write


Output

This example demonstrates basic usage of fwrite to write an array of integers to a binary file.