C fwrite()

The fwrite() function writes count number of objects, each object being size bytes in length, to the stream pointed to by stream from the character array pointed to by buf. The file position indicator is advanced by the number of characters written.

fwrite() Syntax

size_t fwrite(const void *buf, size_t size, size_t count, File *stream);

The fwrite() returns the number of items actually written, which, if the function is successful, will equal the number requested. In case if fewer items are written than are requested, an error has occurred.

fwrite() Example

Following c program writes a float to the file TEST :

Notice here that sizeof is used both to determine the number of bytes in a float and to ensure the portability

void main()
	FILE *fp;
	float f=12.23;
	fp=fopen("test", "wb");
		printf("Error in opening the file..!!\n");
		printf("Press any key to exit..\n");
	fwrite(&f, sizeof(float), 1, fp);

C Online Test

« Previous Function Next Function »

Like/Share Us on Facebook 😋