Name

fputs, puts
- output of characters and strings

Library

libc.lib

Synopsis

  #include <stdio.h>
  int fputs (const char *str, FILE *stream);
  int puts (const char *str);

Return values

The fputs function returns 0 on success and EOF on error; puts returns a nonnegative integer on success and EOF on error.

Detailed description

The function fputs writes the string pointed to by str to the stream pointed to by stream.

The function puts writes the string str, and a terminating newline character, to the stream stdout.


Examples

/****************** this program shows writing characters from a file using fputs **************/
/****************** consider input.txt has the following content: ******************************/
/****************** hello world ****************************************************************/
#include <stdio.h>
int main(void)
{
        int wretval;    
        char rs1[50],rs2[50];
        char *rptr;
        int retval;
        FILE* fp = fopen("c:\\input.txt", "w");
        fprintf(fp, "%s", "abcdefghijklmn");
        fprintf(fp, "%c", "\n");
        fprintf(fp, "%s", "fdsfdsafsdabcdefghijklmn");
        fclose(fp);
        fp = fopen("c:\\input.txt","r");
        if(fp == NULL)
        {
        printf("fopen failed\n");
        return -1;
        }
        rptr = fgets(rs1,12,fp);
        if(rptr == NULL)
        {
        printf("fgets failed\n");
        fclose(fp);
        return -1;
        }
        fclose(fp);
        fp = fopen("c:\\puts1.txt","w+");
        if(fp == NULL)
        {
        printf("fopen failed\n");
        return -1;
        }
        wretval = fputs(rs1,fp);
        if(wretval == EOF)
        {
        printf("fputs failed\n");
        fclose(fp);
        return -1;
        }
        fclose(fp);
        fp = fopen("C:\\puts1.txt","r");
        if(fp == NULL)
        {
        printf("fopen failed\n");
        return -1;
        }
        rptr = fgets(rs2,12,fp);
        if(rptr == NULL)
        {
        printf("fgets failed\n");
        fclose(fp);
        return -1;
        }
        printf("file reading returned \"%s\"\n",rs2);
        fclose(fp);
        unlink("C:\\puts1.txt");
        
        return 0;
}

         

Output

file reading returned "abcdefghijk"

         

Security considerations

The fputs function can be misused by a malicious user to arbitrarily change the functionality of a program under execution using the buffer overflow attack. The buffer overflow attack is possible in the since the function assumes that an infinitely long string is passed to it. Thus users must ensure that they do not overflow the actual space.


Errors

[EBADF]
  The stream argument is not a writable stream.

The functions fputs and puts may also fail and set errno for any of the errors specified for the routines write.


See also

ferror, fputws, putc,


Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top