Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - fwrite()

C Library Function - fwrite()

C Standard Library - <stdio.h> C Standard Library -

Description

C Library Function **size_t fwrite(const void ptr, size_t size, size_t nmemb, FILE stream) Puts the ptr Write the data in the array pointed to by to the given stream stream it.

Declaration

Below is the declaration of the fwrite() function.



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

Parameters

Return Value

If successful, the function returns a size_t object representing the total number of elements; this object is an integer data type. If this number differs from the nmemb argument, an error has occurred.

Example

The following example demonstrates the use of the fwrite() function.

Example

#include

<

stdio.h

int

main

(

)

{

FILE

fp

;

char

str

[

]

=

"

This is runoob.com

"

;

fp

=

fopen

(

"

file.txt

"

,

"

w

"

)

;

fwrite

(

str

,

sizeof

(

str

)

,

1

,

fp

)

;

fclose

(

fp

)

;

return

(

0

)

;

}

Let us compile and run the program above, which will create a file file.txt, with the following content:



This is runoob.com

Now let us use the following program to view the content of the file above:

Example

#include

<

stdio.h

int

main

(

)

{

FILE

fp

;

int

c

;

fp

=

fopen

(

"

file.txt

"

,

"

r

"

)

;

while

(

1

)

{

c

=

fgetc

(

fp

)

;

if

(

feof

(

fp

)

)

{

break

;

}

printf

(

"

%c

"

,

c

)

;

}

fclose

(

fp

)

;

return

(

0

)

;

}

C Standard Library - <stdio.h> C Standard Library -