Elaborating on the title, I'm trying to complete an exercice in K.N. King's C Programming: A Modern Approach which requires me to accomplish the task in the quote below. My idea works, but the program does not return control to the OS. I've nailed the problem down to the do/while loop, which does not seem to reach the EOF mark. So how should I go about detecting the end of file? Thanks in advance!
Write a program that reads a series of phone numbers from a file and displays them in a standard format. Each line of file will contain a single phone number, but the numbers may be in a variety of formats. You may assume that each line contains 10 digits, possibly mixed with other characters (which should be ignored). For example, suppose that the file contains the following lines:
404.817.6900 (215) 686-1776 312-746-6000 877 275 5273 6173434200
The output of the program should have the following appearance:
(404) 817-6900 (215) 686-1776 (312) 746-6000 (877) 275-5273 (617) 343-4200
Have the program obtain the filename from the command line
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void terminate(const char *message);
int main(int argc, char *argv[]) {
FILE *fp;
char str[31], ch;
int i = 0, p1, p2, p3;
if(argc != 2)
terminate("usage: pp2217 <filename>\n");
if(!(fp = fopen(argv[1], "rb")))
terminate("cannot open file for reading\n");
do {
while((ch = getc(fp)) != '\n')
if(isdigit(ch))
str[i++] = ch;
str[i] = '\0';
i = 0;
sscanf(str, "%3d%3d%4d", &p1, &p2, &p3);
printf("(%3d) %3d-%4d\n", p1, p2, p3);
} while(ch != EOF);
fclose(fp);
return 0;
}
void terminate(const char *message) {
fprintf(stderr, "%s", message);
exit(EXIT_FAILURE);
}