5 - Testing is done on producing the output, but the file is still hard coded, and I want the user to pass a file to the program. main is getting kind of long, so I'll refactor the counting code out into a function and test it all again to make sure it still works. Any change, even something tiny, means retesting. Refactoring doesn't change the logic at all, it just restructures the program so that it's easier to manage but still works exactly the same:
Code:
#include <stdio.h>
#include <ctype.h>
void process_file( FILE *fp );
int main( void )
{
FILE *fp = fopen( "test.txt", "r" );
if ( fp ) {
process_file( fp );
fclose( fp );
}
else {
perror( "error opening the file" );
}
return 0;
}
void process_file( FILE *fp )
{
int ch;int nalpha = 0;
int ndigit = 0;
int npunct = 0;
int nspace = 0;
while ( ( ch = fgetc( fp ) ) != EOF ) {
if ( isalpha( ch ) ) {
++nalpha;
}
else if ( isdigit( ch ) ) {
++ndigit;
}
else if ( ispunct( ch ) ) {
++npunct;
}
else if ( isspace( ch ) ) {
++nspace;
}
}
printf( "alphabetic characters: %d\n", nalpha );
printf( "digit characters: %d\n", ndigit );
printf( "punctuation characters: %d\n", npunct );
printf( "whitespace characters: %d\n", nspace );
}
6 - Now I can add the argument stuff for taking a file as a command line parameter without cluttering main up too much. I make sure my tests touch every code path, so I'll fake error and stuff to make sure that the error cases work like I want:
Code:
#include <stdio.h>
#include <ctype.h>
void process_file(FILE * fp);
int main(int argc, char *argv[])
{
if (argc > 1) {
FILE *fp = fopen(argv[1], "r");
if (fp) {
process_file(fp);
fclose(fp);
} else {
perror("error opening the file");
}
} else {
fprintf(stderr, "usage: prog <filename>\n");
}
return 0;
}
void process_file(FILE * fp)
{
int ch;
int nalpha = 0;
int ndigit = 0;
int npunct = 0;
int nspace = 0;
while ((ch = fgetc(fp)) != EOF) {
if (isalpha(ch)) {
++nalpha;
} else if (isdigit(ch)) {
++ndigit;
} else if (ispunct(ch)) {
++npunct;
} else if (isspace(ch)) {
++nspace;
}
}
printf("alphabetic characters: %d\n", nalpha);
printf("digit characters: %d\n", ndigit);
printf("punctuation characters: %d\n", npunct);
printf("whitespace characters: %d\n", nspace);
29,1-8 顶端
}
7 - Now I can really crank down on the stability of the code by adding defensive cases:
Code:
#include <stdio.h>
#include <ctype.h>
int process_file(FILE * fp);
int main(int argc, char *argv[])
{
if (argc > 1) {
FILE *fp = fopen(argv[1], "r");
if (fp) {
if (!process_file(fp)) {
perror("error reading from the file");
}
fclose(fp);
} else {
perror("error opening the file");
}
} else {
fprintf(stderr, "usage: prog <filename>\n");
}
return 0;
}
int process_file(FILE * fp)
{
int ch;
int nalpha = 0;
int ndigit = 0;
int npunct = 0;
int nspace = 0;
int rc = 0;
if (fp != NULL) {
while ((ch = fgetc(fp)) != EOF) {
if (isalpha(ch)) {
++nalpha;
} else if (isdigit(ch)) {
++ndigit;
} else if (ispunct(ch)) {
++npunct;
} else if (isspace(ch)) {
++nspace;
}
}
if (!ferror(fp)) {
printf("alphabetic characters: %d\n", nalpha);
printf("digit characters: %d\n", ndigit);
printf("punctuation characters: %d\n", npunct);
printf("whitespace characters: %d\n", nspace);
rc = 1;
}
}
return rc;
}
8 - Now the code is solid, but there aren't any comments, so I'll go through it and add comments to places that might be confusing and then call it a day. There aren't many because this is a pretty simple program:
Code:
*
File - prog.c
Author - D. Burke (Noir)
Count alphabetic, digit, punctuation, and
whitespace characters in a user supplied file
*/
#include <stdio.h>
#include <ctype.h>
int process_file(FILE * fp);
int main(int argc, char *argv[])
{
if (argc > 1) {
FILE *fp = fopen(argv[1], "r");
if (fp) {
if (!process_file(fp)) {
// failure means a stream error or bad file
perror("error reading from the file");
}
fclose(fp);
} else {
perror("error opening the file");
}
} else {
fprintf(stderr, "usage: prog <filename>\n");
}
return 0;
}
int process_file(FILE * fp)
{
int ch;
int nalpha = 0;
int ndigit = 0;
int npunct = 0;
int nspace = 0;
// assume failure
int rc = 0;
if (fp != NULL) {
while ((ch = fgetc(fp)) != EOF) {
if (isalpha(ch)) {
++nalpha;
} else if (isdigit(ch)) {
++ndigit;
} else if (ispunct(ch)) {
++npunct;
} else if (isspace(ch)) {
++nspace;
}
}
if (!ferror(fp)) {
// only produce output if there are no errors
printf("alphabetic characters: %d\n", nalpha);
printf("digit characters: %d\n", ndigit);
printf("punctuation characters: %d\n", npunct);
printf("whitespace characters: %d\n", nspace);
rc = 1;
}
}
return rc;
}
That's how you should do it too. Start with a skeleton and build the program up bit by bit, ** sure to test after every change. It's okay to change the requirements for testing like when I counted all the characters in the file or just printed the file out. It's okay to backtrack and change your mind on stuff too like when I decided to factor the counting code into a function. It's not as much building a program from a blueprint as it is evolving a program from an idea. You get to change your mind and make it better along the way even after you've finished doing it another way.
|