fgets() and fputs() in C Programming
Hello Everyone, In this article, we will discuss about fgets() and fputs() in C Programming. These functions are used for reading and writing strings to and from files, respectively. By understanding how they work, you can efficiently manage file input and output operations in your programs. Whether you are new to C or have some experience, this guide will help you to understand basics of fgets() and fputs() in C effectively with examples. Let’s get started.
Definition
fgets() and fputs() are two standard library functions in C programming language used for input and output operations respectively.
fgets() Function in C
fgets() is a function that reads a string from a specified file stream or standard input stream (stdin). It reads at most n-1 characters from the stream and stores them into the buffer pointed to by str. The function stops reading when it encounters a newline character (\n) or the end-of-file (EOF) character.
fputs() Function in C
fputs() is a function that writes a string to a specified file stream or standard output stream (stdout). It writes the null-terminated string pointed to by str to the stream.
Syntax of fgets()
The syntax of fgets()
function is as follows:
char *fgets(char *str, int n, FILE *stream);
Here,
str
: a character pointer that represents the string or buffer where the input will be storedn
: an integer that represents the maximum number of characters to be readstream
: a file pointer that represents the input stream
Example of fgets()
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, 100, stdin);
printf("You entered: %s", str);
return 0;
}
In the above example, fgets() function is used to read a string from standard input (stdin) and store it in the str character array. The maximum number of characters to be read is 100. The printf() function is then used to display the entered string on the console.
Syntax of fputs()
The syntax of fputs()
function is as follows:
int fputs(const char *str, FILE *stream);
Here,
str
: a character pointer that represents the string to be writtenstream
: a file pointer that represents the output stream
Example of fputs()
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, 100, stdin);
FILE *fp = fopen("output.txt", "w");
fputs(str, fp);
printf("String written to file successfully.");
fclose(fp);
return 0;
}
In the above example, fgets() function is used to read a string from standard input (stdin) and store it in the str character array. The fopen() function is then used to open a file in write mode and the fputs() function is used to write the string to the file. The fclose() function is used to close the file.
Here is a C program that explains fgets() and fputs() functions:
#include <stdio.h>
int main() {
char str[100]; // declare a character array to store the input string
printf("Enter a string: "); // prompt the user to enter a string
fgets(str, 100, stdin); // read a string from standard input and store it in the str array
printf("You entered: %s", str); // display the entered string on the console
FILE *fp = fopen("output.txt", "w"); // open a file in write mode
if (fp == NULL) { // check if the file is successfully opened or not
printf("Error: Unable to open file."); // display an error message if the file is not opened
return 1;
}
fputs(str, fp); // write the string to the file
fclose(fp); // close the file
printf("String written to file successfully."); // display a success message
return 0;
}
In this program, fgets() function is used to read a string from standard input (stdin) and store it in the str character array. The maximum number of characters to be read is 100. The printf() function is then used to display the entered string on the console.
Then, the fopen() function is used to open a file named “output.txt” in write mode. The fputs() function is used to write the string to the file. The fclose() function is used to close the file.
Finally, printf() function is used to display a success message on the console.