Solution for Exercise 22-5

ex2205

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

int main()
{
    FILE *fh;

    /* "a" == append */
    fh=fopen("hello.txt","a");
    if(fh==NULL)
    {
        puts("Can't open that file!");
        exit(1);
    }
    fprintf(fh,"This text was added later\n");
    puts("Text appended");
    fclose(fh);
    return(0);
}

Output

Text appended

Notes

* If you're using an IDE like Code::Blocks, this code may not find the original hello.txt file for appending. A solution would be to copy it from one project folder to another. Or you can copy all the Code::Blocks executable files to the same folder and run everything there.