You can use the fopen, fprint and fclose commands to write output to a file instead of the screen.
To begin, you need to open the file and associate it with a variable. You use fopen for this, which takes a file name as argument. For example,
will create a file named “bar” (and so erase it if it already exists). You can use the fprint command to write to the file; it takes the variable representing the file as the first argument, followed by what you want to write to the file. For example, if x is equal to 9, then
will put
"x + 1 is "10
in the file. Note that the quotation marks are inserted with the string. If you want to insert strings without quotes, then you can give fprint a second argument of Unquoted. If instead of the above printf you entered
then the file would contain
x + 1 is 10
Finally, after you have finished writing what you want into the file, you close the file with the fclose command,