Solution for Exercise 22-2

ex2202

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

int main()
{
    FILE *fh;
    int ch;

    fh=fopen("hello.txt","r");
    if(fh==NULL)
    {
        puts("Can't open that file!");
        exit(1);
    }
    while((ch=fgetc(fh))!=EOF)
        putchar(ch);
    fclose(fh);
    return(0);
}

Notes

* To properly locate the hello.txt file, it must be created in or copied to the same folder as this project's source code. For example, in Code::Blocks, this source code file (usually main.c) must be in the same folder as hello.txt.

* To access the file at another location, include the full pathame. For example:

C:/Users/Dan/Documents/hello.txt

Or in Windows, remember to double-up on the backslashes for the pathname:

C:\\Users\\Dan\\Documents\\hello.txt

You can include spaces in the pathname; they don't need to be escaped because the pathname is enclosed in double quotes.