Load the file named readchar.c and display it on your monitor. This is our first program to read a file.
#include "/sys/stdio.h"
main( )
{
FILE *funny;
int c;
funny = fopen("TENLINES.TXT","r");
if (funny == NULL) printf("File doesn’t exist\n");
else {
do {
c = getc(funny); /* get one character from the file */
putchar(c); /* display it on the monitor */
} while (c != EOF); /* repeat until EOF (end of file) */
}
fclose(funny);
}
This program begins with the familiar "include", some data definitions, and the file opening statement which should require no explanation except for the fact that an "r" is used here because we want to read it. In this program, we check to see that the file exists, and if it does, we execute the main body of the program. If it doesn’t, we print a message and quit. If the file does not exist, the system will set the pointer equal to NULL which we can test.
The main body of the program is one "do while" loop in which a single character is read from the file and output to the monitor until an EOF (end of file) is detected from the input file. The file is then closed and the program is terminated. CAUTION CAUTION CAUTION
At this point, we have the potential for one of the most common and most perplexing problems of programming in C. The variable returned from the "getc" function is a character, so we could use a "char" variable for this purpose. There is a problem with that however, because on some, if not most, implementations of C, the EOF returns a minus one which a "char" type variable is not capable of containing. A "char" type variable can only have the values of zero to 255, so it will return a 255 for a minus one on those compilers that use a minus one for EOF. This is a very frustrating problem to try to find because no diagnostic is given. The program simply can never find the EOF and will therefore never terminate the loop. This is easy to prevent, always use an "int" type variable for use in returning an EOF. You can tell what your compiler uses
for EOF by looking at the "stdio.h" file where EOF is defined. That is the standard place to define such values
There is another problem with this program but we will worry about it when we get to the next program and solve it with the one following that. After you compile and run this program and are satisfied with the results, it would be a good exercise to change the name of "TENLINES.TXT" and run the program again to see that the NULL test actually works as stated. Be sure to change the name back because we are still not
finished with "TENLINES.TXT".
#include "/sys/stdio.h"
main( )
{
FILE *funny;
int c;
funny = fopen("TENLINES.TXT","r");
if (funny == NULL) printf("File doesn’t exist\n");
else {
do {
c = getc(funny); /* get one character from the file */
putchar(c); /* display it on the monitor */
} while (c != EOF); /* repeat until EOF (end of file) */
}
fclose(funny);
}
This program begins with the familiar "include", some data definitions, and the file opening statement which should require no explanation except for the fact that an "r" is used here because we want to read it. In this program, we check to see that the file exists, and if it does, we execute the main body of the program. If it doesn’t, we print a message and quit. If the file does not exist, the system will set the pointer equal to NULL which we can test.
The main body of the program is one "do while" loop in which a single character is read from the file and output to the monitor until an EOF (end of file) is detected from the input file. The file is then closed and the program is terminated. CAUTION CAUTION CAUTION
At this point, we have the potential for one of the most common and most perplexing problems of programming in C. The variable returned from the "getc" function is a character, so we could use a "char" variable for this purpose. There is a problem with that however, because on some, if not most, implementations of C, the EOF returns a minus one which a "char" type variable is not capable of containing. A "char" type variable can only have the values of zero to 255, so it will return a 255 for a minus one on those compilers that use a minus one for EOF. This is a very frustrating problem to try to find because no diagnostic is given. The program simply can never find the EOF and will therefore never terminate the loop. This is easy to prevent, always use an "int" type variable for use in returning an EOF. You can tell what your compiler uses
for EOF by looking at the "stdio.h" file where EOF is defined. That is the standard place to define such values
There is another problem with this program but we will worry about it when we get to the next program and solve it with the one following that. After you compile and run this program and are satisfied with the results, it would be a good exercise to change the name of "TENLINES.TXT" and run the program again to see that the NULL test actually works as stated. Be sure to change the name back because we are still not
finished with "TENLINES.TXT".

