Program 6 of Tim Swenson’s C Page: a simple version of the Unix utility wc. It asks for a file name, reads the file a character at a time and reports the number of lines, words and characters. Written for a QL C compiler whose header file is stdio_h (YES and NO are defined there, so the #define lines are commented out).
The column explains the multiple assignment nl = nw = nc = 0, character constants such as ‘\n’ and ‘\t’, and the %d format of printf. This is C source, not a SuperBASIC program, so there is no download; type it in and compile it with a QL C compiler.
Published in Sinc-Link January-February 1991 (The C Page).
Source Code
/* Program 6 */
#include <stdio_h>
/* #define YES 1
#define NO 0
The two constants are defined in stdio_h for this
implementation, but may not be for others
*/
main () {
int c, fd, nl, nw, nc, word;
char file[20];
printf("Enter File: ");
gets(file);
fd = fopen(file,"r");
if (fd == NULL) {
printf("File Open Error");
abort(1);
}
word = NO;
nl = nw = nc = 0;
while (( c = getc(fd)) != EOF) {
++nc;
if ( c == '\n')
++nl;
if ( c == ' ' || c == '\n' || c == '\t')
word = NO;
else if (word == NO) {
word = YES;
++nw;
}
}
fclose(fd);
printf("Lines: %d Words: %d Chars: %d",nl,nw,nc);
}Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.