C Programming language

adplus-dvertising
String I/O : How to use fputs( ) Function in C
Previous Home Next
char * fputs(const char *str , FILE *stream);

The fputs( ) function writes the content of the string pointed to by str to the specified stream. The null terminator is not written. The fputs( ) function returns the last character written on success and EOF on failure.

/** program to understand the use of fputs( ) */ #include<stdio.h>
void main()
{
FILE *fp1;
char str[100];
fp1=fopen("rajesh.txt","w");
printf("enter the text\n")
if((gets(str)!=NULL))
fputs(str,fp1);
fclose(fp1);
};

Output: Suppose we enter this text after running the program:

Ashish Kumar Gupta

Master of computer application

Press ctrl Z when the first line of text is entered and enter key is pressed the functions gets() converts the newline character and array str contains "Ashish Kumar Gupta"(18 character + 1 null character). the null character is not written to the file , so only 18 character are written.

Previous Home Next