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);
Table 12.2. The different signifiers used with printf(), sprintf(), and fprintf().
fprintf() Signifiers
SignifierMeaning
dinteger
ffloating point number
hdshort integer
ldlong integer
huunsigned short integer
uunsigned integer
luunsigned ...

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.