Function fputs () in C

The function int fputs (const char * str, FILE * stream) in the Standard C library writes a string to the specified Stream (not writing null characters).

The function int fputs (const char * str, FILE * stream) in the Standard C library writes a string to the specified Stream (not writing null characters).

Declare function fputs () in C

Below is the declaration for fputs () in C:

 int fputs ( const char * str , FILE * stream ) 

Parameters

str - This is an array containing the sequence of characters ending with null written.

stream - This is the pointer to a FILE object that identifies the Stream, where the string is written.

Returns the value

This function returns a non-negative value, or returns EOF if there is an error.

For example

The following program C illustrates the usage of fputs () in C:

 #include int main () { FILE * fp ; fp = fopen ( "baitapc.txt" , "w+" ); fputs ( "Hoc c co ban va nang cao tai QTM." , fp ); fputs ( "Loat bai thu vien C chuan." , fp ); fclose ( fp ); return ( 0 ); } 

Compiling and running the above program will create a baitcap.txt in the current directory.

Now follow the contents of the above file by using the following C program:

 #include int main () { FILE * fp ; int c ; fp = fopen ( "baitapc.txt" , "r" ); while ( 1 ) { c = fgetc ( fp ); if ( feof ( fp ) ) { break ; } printf ( "%c" , c ); } fclose ( fp ); return ( 0 ); } 

Compile and run the above program to see the results.

According to Tutorialspoint

Last lesson: Function fputc () in C

Next lesson: Function getc () in C

4 ★ | 1 Vote