Writing to files with streams
In this recipe, we will write to files using file streams instead of file descriptors, as we did in earlier recipes.
As with the previous recipes where we had already seen file descriptors 1, 2, and 3, and some of their system calls, we have already seen file streams too, such as some of the printUsage()
functions we have created. Some of these functions we created took two arguments, the first one being declared as FILE *stream
. The argument we provided was stderr or stdout.
But we can also use file streams to write to files, which we will do in this recipe.
As you probably have noticed by now, some things keep coming again and again, such as file descriptors and file streams.
Working with file streams instead of file descriptors has some advantages. For example, with file streams, we can use functions such as fprintf()
to write to files. This means that there are more—and more powerful—functions to read and write data.