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

C Library Function - fputc()

C Library Function - fputc()

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

Description

C library function *int fputc(int char, FILE stream) Converts the string pointed to by the argument char Write the specified character (an unsigned char) to the specified stream stream and advance the position indicator.

Declaration

Below is the declaration of the fputc() function.



int fputc(int char, FILE *stream)

Parameters

Return Value

If no error occurs, it returns the character that was written. If an error occurs, it returns EOF and sets the error indicator.

Example

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

Example

#include

<

stdio.h

int

main

(

)

{

FILE

fp

;

int

ch

;

fp

=

fopen

(

"

file.txt

"

,

"

w+

"

)

;

for

(

ch

=

33

;

ch

<=

100

;

ch

++

)

{

fputc

(

ch

,

fp

)

;

}

fclose

(

fp

)

;

return

(

0

)

;

}

Let's compile and run the above program; this will create a file in the current directory file.txt, with the following content:



!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd

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 -