On this page
fprintf
Prev: printf | Up: stdio.h | Next: snprintf
Rel: stdio.h, stdio.h & File I/O Guide
Ref: cppreference — fprintf
Tags: #public #prog #c #stdlib #stdio #tier/core
Include
#include <stdio.h>
Signature & Overview
int fprintf(FILE *restrict stream, const char *restrict fmt, ...);
Writes formatted output to a specific FILE * stream (such as stderr or an open file).
Notes & Pitfalls
- Never pass user input as the format string (
fprintf(f, user_str)): Always writefprintf(f, "%s", user_str)to prevent format-string vulnerabilities.
Example
#include <assert.h>
#include <stdio.h>
int main(void) {
FILE *f = tmpfile();
assert(fprintf(f, "port=%d\n", 8080) > 0);
fclose(f);
}
Linked from
Related notes
- tuts/c/stdlib/stdio/printf Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/snprintf Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib
- tuts/c/stdlib/stdio_and_file_io Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stdio/fclose shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/feof shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/ferror shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fflush shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fgets shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fopen shared tags: #c, #prog, #stdio, #stdlib, #tier