fprintf
?????????????????? C ????
-
????????????:
#include
int main() { int a = 42; float b = 3.14; const char *s = "Hello, World!"; FILE *file = fopen("output.txt", "w"); if (file == NULL) { printf("Error opening file.\n"); return 1; } fprintf(file, "Integer: %d\n", a); fprintf(file, "Float: %.2f\n", b); fprintf(file, "String: %s\n", s); fclose(file); return 0; } -
???????????:
fprintf(file, "Hexadecimal: %x\n", a); fprintf(file, "Octal: %o\n", a);
-
??????:
void *ptr = &a; fprintf(file, "Pointer address: %p\n", ptr);
-
??????????:
fprintf(file, "With leading zeros: d\n", a);
-
??????????????:
fprintf(file, "Left-aligned: %-10s\n", s); fprintf(file, "Right-aligned: s\n", s);
-
?????????????:
fprintf(file, "Scientific notation: %e\n", b);
-
????????(
va_list
)????????????:#include
void custom_fprintf(FILE *file, const char *format, ...) { va_list args; va_start(args, format); vfprintf(file, format, args); va_end(args); } int main() { // ... (open the file and declare variables) custom_fprintf(file, "Integer: %d\n", a); custom_fprintf(file, "Float: %.2f\n", b); custom_fprintf(file, "String: %s\n", s); // ... (close the file) }
??????????????? fprintf
??????,??????,?????????????(???????????)???????