Character I/O includes getc(), putc(), fgetc(), fputc() functions. Those functions we are going to discuss in this blog.
file pointer pointing any location
Basically, the file pointer is a pointer to a structure and when it points to any location. It contains some information like file is being read/append/written, end of file reached or not, any error has occurred or not, the name of the file, the position of file pointer,etc.
putc()
This function is used to write any character into a file. This does not allow the whole string but if we want to write a whole string into the file using this function then we have to line by line characters of a string to this function.
After writing any character (or reading), the pointer will point to the next location.
putc(‘<character that we want to pass>’,<file pointer name>);
Suppose we want to make file pointer fp1 to point to a text file having the name f1.txt. and we want to write K into file f1.txt then below is an example of putc syntax.
putc(‘K’,fp1);
getc()
This function is used to read any character from a file. This will read the character from the file from that location, which file pointer is pointing. Suppose the file pointer is pointing to location 4 then getc will read the character which is written at location 4.
If we want to read data from the starting of the file then we have to make a pointer to point at the starting of the file. (For that we can use the rewind function.
rewind(<file pointer name>) )
<character variable> = getc(<file pointer name>);
If we use the same previous example then below is the syntax for getc. And c is a character variable. After using the below syntax if we print the value of c then it will print the character from the location which is pointed by file pointer fp1.
c = getc(fp1);
fputc(), fgetc()
The operations and syntax of fgetc( ) and fputc( ) are exactly similar to that of getc( ) and putc( ), the only difference is that the fgetc( ), fputc( ) are defined as functions while the last two are macros in library.
Syntax of fputc : fputc(‘<character that we want to pass>’,<file pointer name>); Syntax of fgetc : <character variable> = fgetc(<file pointer name>);
Example program structure to understand the use putc and getc
In the below example, f1 is a file name and fp1 is a file pointer. First, we have used fopen with mode w+ because we want to write using putc and read using getc. Then we have used putc to write the character ‘K’ in the f1 file. Now pointer fp1 is pointing to the location after ‘K’ because it has already written ‘K’ at that location so now it is pointing to the location after ‘K’.
But if we want to read ‘K’ then the pointer should point to the location where ‘K’ is written so we are using the rewind function to set the pointer at the beginning of the file. Now we are using putc to print the character at the beginning which is ‘K’.
#include<stdio.h> #include<conio.h> void main() { FILE *fp1; char c; clrscr(); fp1=fopen("f1.txt","w+"); putc('K',fp1); rewind(fp1); c=getc(fp1); printf("character = %c ",c); fclose(fp1); getch(); }
Output:
Learning from this blog;
- Use of putc and getc.
- The syntax for putc and getc.
- What is the use of the rewind function?
- Difference between fputc, fgetc, and putc, getc.
- Write a program using putc and getc.