C Library Function - fputc()
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
- char -- This is the character to be written. The character is passed as its corresponding int value.
- stream -- This is a pointer to a FILE object that identifies the stream to which the character is to be written.
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 -