The CI/O subset of the C++ standard library implements C-style stream input/output operations. The <cstdio> header file provides general file support and provides functions with narrow and multibyte character input/output capabilities, while the <cwchar> header file provides functions with wide character input/output capabilities.
Unformatted input/output
Write characters to file stream
std::fputc,
std::putc
int fputc( int ch, std::FILE* stream ); |
Write characters ch
to the given output stream stream
.
Internally, characters are converted to unsigned char before writing.
In C, putc() can be implemented as a macro, which is prohibited in C++. Thus calling std::fputc() and std::putc() always have the same effect.
parameter
ch | - | characters to write |
stream | - | output stream |
return value
On success, returns the character written.
On failure, returns EOF and sets the error indicator on the stream (see std::ferror()).
Call example
#include <cstdio>
int main()
{
for (char c = 'a'; c != 'z'; c++)
{
std::putc(c, stdout);
}
std::putc('\n', stdout);
// putchar 的返回值不等于参数
int r = 0x1070;
std::printf("\n0x%x\n", r);
r = std::putchar(r);
std::printf("\n0x%x\n", r);
return 0;
}
output
Write string to file stream
std::fputs
int fputs( const char* str, std::FILE* stream ); |
Write str
each character from the null-terminated string to stream
, as by repeatedly executing std::fputc.
str
The terminating null character from is not written .
parameter
str | - | null terminated string to write |
stream | - | output stream |
return value
On success, returns a non-negative value.
On failure, returns EOF and sets the errorstream
indicator on (see std::ferror).
Notice
The related function std::puts appends a newline character to the output and std::fputs
writes an unmodified string.
Different implementations return different non-negative numbers: some return the last character written, some return the number of characters written (or this value if the string is longer than INT_MAX), some simply a non-negative constant, such as zero.
Call example
#include <cstdio>
int main(void)
{
int rc = std::fputs("Hello World", stdout);
if (rc == EOF)
{
std::perror("fputs()"); // POSIX 要求设置 errno
}
return 0;
}