Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io/c/fputs"

From cppreference.com
< cpp‎ | io‎ | c
m (fmt)
 
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{cpp/title | fputs}}
+
{{cpp/title|fputs}}
{{cpp/io/c/sidebar}}
+
{{cpp/io/c/navbar}}
{{ddcl | header=cstdio |
+
{{ddcl|header=cstdio|
int fputs( const char *str, FILE *stream );
+
int fputs( const char* str, std::FILE* stream );
 
}}
 
}}
  
Writes given null-terminated character string to the given output stream.  
+
Writes every character from the null-terminated string {{tt|str}} to the output stream {{tt|stream}}, as if by repeatedly executing {{lc|std::fputc}}.
 +
 
 +
The terminating null character from {{tt|str}} is not written.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | str | null-terminated character string to be written}}
+
{{par|str|null-terminated character string to be written}}
{{param list item | stream | output stream}}
+
{{par|stream|output stream}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
 +
On success, returns a non-negative value
  
non-negative integer on success, {{c|EOF}} on failure
+
On failure, returns {{lc|EOF}} and sets the ''error'' indicator (see {{lc|std::ferror}}) on {{tt|stream}}.
 +
 
 +
===Notes===
 +
The related function {{lc|std::puts}} appends a newline character to the output, while {{tt|std::fputs}} writes the string unmodified.
 +
 
 +
Different implementations return different non-negative numbers: some return the last character written, some return the number of characters written (or {{lc|INT_MAX}} if the string was longer than that), some simply return a non-negative constant such as zero.
 +
 
 +
===Example===
 +
{{example|code=
 +
#include <cstdio>
 +
 +
int main(void)
 +
{
 +
    int rc = std::fputs("Hello World", stdout);
 +
 
 +
    if (rc == EOF)
 +
        std::perror("fputs()"); // POSIX requires that errno is set
 +
}
 +
|output=
 +
Hello World
 +
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/io/c/dcl list fprintf}}
+
{{dsc inc|cpp/io/c/dsc fprintf}}
{{dcl list template | cpp/io/c/dcl list puts}}
+
{{dsc inc|cpp/io/c/dsc puts}}
{{dcl list template | cpp/io/c/dcl list fgets}}
+
{{dsc inc|cpp/io/c/dsc fputws}}
{{dcl list end}}
+
{{dsc inc|cpp/io/c/dsc fgets}}
 +
{{dsc see c|c/io/fputs}}
 +
{{dsc end}}
  
[[fr:cpp/io/c/fputs]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[ja:cpp/io/c/fputs]]
+
[[zh:cpp/io/c/fputs]]
+

Latest revision as of 12:56, 29 November 2022

 
 
 
C-style I/O
Types and objects
Functions
File access
Direct input/output
Unformatted input/output
Formatted input
(C++11)(C++11)(C++11)    
(C++11)(C++11)(C++11)    
 
Defined in header <cstdio>
int fputs( const char* str, std::FILE* stream );

Writes every character from the null-terminated string str to the output stream stream, as if by repeatedly executing std::fputc.

The terminating null character from str is not written.

Contents

[edit] Parameters

str - null-terminated character string to be written
stream - output stream

[edit] Return value

On success, returns a non-negative value

On failure, returns EOF and sets the error indicator (see std::ferror) on stream.

[edit] Notes

The related function std::puts appends a newline character to the output, while std::fputs writes the string unmodified.

Different implementations return different non-negative numbers: some return the last character written, some return the number of characters written (or INT_MAX if the string was longer than that), some simply return a non-negative constant such as zero.

[edit] Example

#include <cstdio>
 
int main(void)
{
    int rc = std::fputs("Hello World", stdout);
 
    if (rc == EOF)
        std::perror("fputs()"); // POSIX requires that errno is set
}

Output:

Hello World

[edit] See also

prints formatted output to stdout, a file stream or a buffer
(function) [edit]
writes a character string to stdout
(function) [edit]
writes a wide string to a file stream
(function) [edit]
gets a character string from a file stream
(function) [edit]
C documentation for fputs