fputs() — Write a String
| Standards / Extensions | C or C++ | Dependencies |
|---|---|---|
ISO C
POSIX.1 XPG4 XPG4.2 |
both |
Format
#include <stdio.h>
int fputs(const char *string, FILE *stream);
General Description
Writes the string pointed to by string to the output stream
pointed to by stream. It does not write the terminating \0 at
the end of the string.
For a text file, truncation may occur if the record is too long. Truncation means that excess characters are discarded after the record is full, up to a control character that ends the line (\n). Characters after the \n start at the next record. For more information about truncation, see z/OS: XL C/C++ Programming Guide.
The fputs() function is not supported for files opened with type=record.
The fputs()function has the same restriction as any write operation for a read immediately following a write or a write immediately following a read. Between a write and a subsequent read, an intervening flush or reposition must occur. Between a read and a subsequent write, an intervening flush or reposition must also occur, unless an EOF has been reached.
Returned Value
If successful, fputs() returns the number of bytes written.
If unsuccessful, fputs() returns EOF.
Example
CBC3BF35
/* CBC3BF35
This example writes a string to a stream.
*/
#include <stdio.h>
#define NUM_ALPHA 26
int main(void)
{
FILE * stream;
int num;
/* Do not forget that the '/0' char occupies one character */
static char buffer[NUM_ALPHA + 1] = "abcdefghijklmnopqrstuvwxyz";
if ((stream = fopen("myfile.dat", "w")) != NULL )
{
/* Put buffer into file */
if ( (num = fputs( buffer, stream )) != EOF )
{
/* Note that fputs() does not copy the /0 character */
printf( "Total number of characters written to file = %i\n", num );
fclose( stream );
}
else /* fputs failed */
printf( "fputs failed" );
}
else
printf( "Error opening myfile.dat" );
}