codescracker


c

C fwrite()



« Previous Function Next Function »


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

#include<stdio.h>
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

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
	clrscr();
	FILE *fp;
	float f=12.23;
	fp=fopen("test", "wb");
	if(fp==NULL)
	{
		printf("Error in opening the file..!!\n");
		printf("Press any key to exit..\n");
		getch();
		exit(1);
	}
	fwrite(&f, sizeof(float), 1, fp);
	fclose(fp);
	getch();
}

« Previous Function Next Function »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test