Solution for Exercise 22-3

ex2203

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fh;

    /* open the file */
    fh=fopen("hello.txt","w");
    /* check for errors */
    if(fh==NULL)
    {
        puts("Can't open that file!");
        exit(1);
    }
    /* output text */
    fprintf(fh,"Look what I made!\n");
    fputs("My C program wrote this file.\n",fh);
    /* close the file */
    fclose(fh);
    /* inform the user */
    puts("File written.");
    return(0);
}

Output

File written.

Notes

* If you run the solution for Exercise 22-2 after running this Exercise's program, you see this output:

* The standard I/O puts() function is often a macro assigned to the fputs() function with stdout as the file handle.