O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required