fputs() Function in C
Last updated on July 27, 2020
The syntax of fputs()
function is:
Syntax: int fputc(const char *str, FILE *fp);
This function is used to print a string to the file. It accepts two arguments pointer to string and file pointer. It writes a null-terminated string pointed by str
to a file. The null character is not written to the file. On success, it returns 0
. On error, it returns EOF
or -1
.
The following program demonstrates how to use fputs()
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include<stdio.h>
#include<stdlib.h>
int main()
{
char str[50];
FILE *fp;
fp = fopen("myfile2.txt", "w");
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
printf("Testing fputs() function: \n\n");
printf("To stop reading press Ctrl+Z in windows and Ctrl+D in Linux :");
while( gets(str) != NULL )
{
fputs(str, fp);
}
fclose(fp);
return 0;
}
|
Expected Output:
1 2 3 4 5 6 7 8 | Testing fputs() function:
To stop reading press Ctrl+Z in windows and Ctrl+D in Linux :
The first line
The second line
Third line
^D
|
How it works:
In line 6, an array of character str
of size 50
is declared.
In line 7, a structure pointer variable fp
of type struct FILE
is declared.
In line 8, fopen()
function is called with two arguments namely "myfile2.txt"
and "w"
. On success, it returns a pointer to file myfile2.txt
and opens the file myfile.txt
in write-only mode. On failure or end of file, it returns NULL
.
In lines 10-14, if statement is used to test the value of fp
. If it is NULL
, printf()
statement prints the error message and program terminates. Otherwise, the program continues with the statement following the if statement.
In line 16 and 17, two printf()
statements string "Testing fputs() function: \n\n"
and "To stop reading press Ctrl+Z in windows and Ctrl+D in Linux : \n\n"
to the console.
In lines 19-22, we have while loop in conjunction with gets()
function. The while loop will keep asking for more strings when until it counters an end of file character. Here are two important things to remember about the gets() function:
gets()
function converts newline character entered to null character ('\0'
).- When the end of file character is encountered
gets()
returnsNULL
.
Here is how while loop works:
When first line "The first line"
is entered followed by the newline, the gets()
function converts the newline('\n'
) to null character('\0'
). At this point, str
contains "The first line\0"
, which is then written to the file.The fputs()
function is then used to write the string to the file. It is important to note that fputs()
function do not writes the null character '\0'
character is to the file. When gets()
function encounters the end of file character the while condition becomes false and control comes out of the loop.
In line 24, fclose()
function closes the file pointer.
Difference between puts() and fputs() #
Recall that in earlier chapters we have used puts()
function several times to print the strings to the console. The important difference between fputs()
and puts()
is that, the puts()
converts the null character('\0'
) in the string to the newline ('\n'
) character whereas fputs()
doesn't not.
Load Comments