Writing to Files
Once you've opened a file for writing (meaning that you used either the w or a mode, or a variant thereof), you can write data to it. To do so, you can use the fprintf() function, which works like printf() except that it takes a file pointer as its first argument:
fprintf (file_pointer, formatting, extra_arguments);
The formatting signifiers are exactly the same as those used with printf() and scanf() (Table 12.2). So, to write an unsigned integer to a file, you would code
fprintf (fp, "%u", 30);
fprintf() Signifiers | |
---|---|
Signifier | Meaning |
d | integer |
f | floating point number |
hd | short integer |
ld | long integer |
hu | unsigned short integer |
u | unsigned integer |
lu | unsigned ... |
Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.