Character Input / Output in file
putc()
This
function is used to write a character to a file. It works same like as putchar()
function and handle one character at a
time.
Syantax:
int putc(int
char, FILE *
stream);
Here, the file pointer indicates the file to write to. the char is called an integer in putc().
On success the
character is returned. If an error occurs, the error indicator for the stream
is set and EOF
is returned.
For every operation of
putc() function , the file pointer moves position of character one by one.
Ex,
FILE
*fp;
fp =
fopen(“book.txt”, “w”);
char
ch=’r’;
putc(ch,fp);
getc()
This
function is used to read a character from a
file. The file has been open in read mode. It works same like as
getchar() function and handle one character at
a time.
Synatx:
int getc(File_ptr); OR
int getc(FILE *
stream);
Here , Gets the next
character (an
unsigned char
) from the specified
stream and advances the position indicator for the stream.
For every operation of
getc() function , the file pointer moves position of character one by one.
On success the character is
returned. If the end-of-file is encountered, then
EOF
is returned and the end-of-file indicator is set.
If an error occurs then the error indicator for the stream is set and EOF
is returned.
Ex,
FILE
*fp;
fp =
fopen(“book.txt”, “r”);
char;
ch=getc(fp);
fputc()
This function is used to write a character to a file.
Syntax:
int fputc(int
char, FILE *
stream);
Here, first argument
specifies the character that wanted to write in file. Second argument is the
pointer to file which stores address of the location of file.
On success the character is returned.
If an error occurs, the error indicator for the stream is set and
EOF
is returned.
To use
fputc() file must be opened in any available mode
Ex,
FILE
*fp;
fp =
fopen(“book.txt”, “w”);
char
ch=’r’;
fputc(ch,fp);
fgetc()
This function is used to read content of file from memory.
Syntax:
int fgetc(File_ptr);
Here, File_ptr is the
pointer to file.
It reads the character
from the current position and
moves pointer to next position so that it now pointes to the next character.
On success the character is
returned. If the end-of-file is encountered, then
EOF
is returned and the end-of-file indicator is set.
If an error occurs then the error indicator for the stream is set and EOF
is returned.
Ex,
FILE
*fp;
fp =
fopen(“book.txt”, “r”);
char;
ch=fgetc(fp);
Posted By : Ruchita Pandya
No comments:
Post a Comment