fgetc()
fgetc() is used to obtain input from a file single character at a time. This function returns the number of characters read by the function. It returns the character present at position indicated by file pointer. After reading the character, the file pointer is advanced to next character. If pointer is at end of file or if an error occurs EOF file is returned by this function.
Syntax:
int fgetc(FILE *pointer) pointer: pointer to a FILE object that identifies the stream on which the operation is to be performed.
// C program to illustate fgetc() function #include <stdio.h> int main () { // open the file FILE *fp = fopen("test.txt","r"); // Return if could not open file if (fp == NULL) return 0; do { // Taking input single character at a time char c = fgetc(fp); // Checking for end of file if (feof(fp)) break ; printf("%c", c); } while(1); fclose(fp); return(0); } |
Output:
The entire content of file is printed character by character till end of file. It reads newline character as well.
Using fputc()
fputc() is used to write a single character at a time to a given file. It writes the given character at the position denoted by the file pointer and then advances the file pointer.
This function returns the character that is written in case of successful write operation else in case of error EOF is returned.
Syntax:
int fputc(int char, FILE *pointer) char: character to be written. This is passed as its int promotion. pointer: pointer to a FILE object that identifies the stream where the character is to be written.
// C program to illustate fputc() function #include<stdio.h> int main() { int i = 0; FILE *fp = fopen("output.txt","w"); // Return if could not open file if (fp == NULL) return 0; char string[] = "good bye", received_string[20]; for (i = 0; string[i]!='\0'; i++) // Input string into the file // single character at a time fputc(string[i], fp); fclose(fp); fp = fopen("output.txt","r"); // Reading the string from file fgets(received_string,20,fp); printf("%s", received_string); fclose(fp); return 0; } |
Output:
good bye
When fputc() is executed characters of string variable are written into the file one by one. When we read the line from the file we get the same string that we entered.
This article is contributed by Hardik Gaur. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- What is return type of getchar(), fgetc() and getc() ?
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- fesetround() and fegetround() in C++ and their application
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Similarities and Differences between Ruby and C language
- Code Optimization Technique (logical AND and logical OR)
- Getting System and Process Information Using C Programming and Shell in Linux
- INT_MAX and INT_MIN in C/C++ and Applications
- Difference and Similarities between PHP and C
- What are the differences between bitwise and logical AND operators in C/C++?
- Change/add only one character and print '*' exactly 20 times
- Comma in C and C++
- Return values of printf() and scanf() in C/C++
- Difference Between malloc() and calloc() with Examples
- How does "void *" differ in C and C++?
- exit(), abort() and assert()
- What are the operators that can be and cannot be overloaded in C++?
- Write one line functions for strcat() and strcmp()
- Data type of character constants in C and C++
- Can we use function on left side of an expression in C and C++?

