Updated April 1, 2023
Introduction to fputs in C
In this article, we are discussing C library function for reading and writing string from a stream such as fputs and fgets functions for reading and writing files. We are elaborating the function fputs in C programming language which used for writing or printing string or array of characters to the specified stream and this stream will not include the null character and hence the null character is not written to the file. This function writes the string to the file which accepts two arguments one pointer to the string and another argument as a file pointer. So when the string is written successfully to the file then it returns 0 else if any error occurs then it will return EOF or -1.
Examples of fputs Library Function in C
In C programming language, there are functions for writing and reading a string from the stream and they are fputs() and fgets(). These functions are fputs() for writing the string or array of characters to the string and fgets() function is for reading set of characters or string from the file. This fputs function is a library function for writing the string or set of characters to the file which outputs a string to a stream in file handling. This function accepts the string or array of characters from the user and it will be stored in the input stream and to accept the next string the file pointer is incremented. Now let us the syntax of this function.
Syntax:
int fputs(const char *s, FILE *stream)
Parameters:
- s: it is a set of characters or an array of characters with excluding the null character.
- stream: this points to the file object FILE which indicates the stream of the string to be written to the file.
The fputs() function returns the non-negative value that is 0 if true else it will return EOF for error or -1.
Example #1
Code:
#include <stdio.h>
int main()
{
//file pointer
FILE *fp = NULL;
fp = fopen("aticle.txt", "w");
if(fp == NULL)
{
printf("Error in creating the file\n");
exit(1);
}
fputs("Hello Educba Training, Welcome",fp);
//close the file
fclose(fp);
printf("File has been created successfully\n");
return 0;
}
Output:
File is created with the name article.txt and in that file it will write the message as below.
In the above program, we will be creating the file in write mode so that after creating the file we can write the message to that file using fputs() function and it is necessary to close the file after writing the message.
In C programming, there are two functions for writing the string or array of characters to the output screen or file through puts() or fputs() functions respectively. These functions are used for particular reasons, where: puts function uses single argument and fputs function accepts two arguments because fputs is for file handling and puts is for just printing the message on the message is printed in the file. fputs() stands for file put string which you can find in C standard library header file known as stdio.h. We can see the example below for printing two lines in the file where fputs() will not append the newline until it is manually written.
Example #2
Code:
#include <stdio.h>
int main()
{
FILE *f = NULL;
f = fopen("aticle.txt", "w");
if(f == NULL)
{
printf("Error in creating the file\n");
exit(1);
}
fputs("Writing the first Line in the file.",f);
fputs("Writing the Second Line in the file.",f);
fclose(f);
puts("Writing the first Line on the output screen.");
puts("Writing the second Line on the output screen.");
return 0;
}
Output:
Message printed in the file article.txt
In the above program, we can see we are creating a file to write or print the message using fputs() function where we are writing two lines to the file, we can see in the file article.txt which was created has the two lines printed but there is no newline appended after one line is printed to the file this can be seen in the second screenshot where we can see the content of the article.txt file. But whereas puts() function appends the newline by default for each puts() statement this can be seen when using puts() function it prints each line or new line on the console as we can see in the first screenshot. So the puts() function converts the null character (‘\0’) in the string to the newline whereas in fputs() the null character is not included and it also does not append any newline as done in puts() function.
Conclusion
In this article, we have discussed the writing function to the file, especially for file handling which is known as fputs(). In C programming language there are two functions for writing puts() and fputs(). In C, puts() function is to write the message to the console or window terminal, whereas fputs() function is to write the messages to the file. These two functions are different as to puts() function automatically appends the newline character whereas fputs do not append any newline. In this article, fputs() function will also take the string or array of characters until null character to print to the file, which means the fputs() function will not write null character i.e. fputs() will terminate the null character and then prints the string or array of characters to the file. This function is usually used during the file handling and other such function for reading string or array of characters from the file we have fgets() function.
Recommended Articles
This is a guide to fputs in C. Here we also discuss the Introduction and working of fputs library function in c along with examples and its code implementation. You may also have a look at the following articles to learn more –