Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Basic C implementation of "wc -w" */
- #include <ctype.h>
- #include <stdio.h>
- int exitstatus = 0;
- void wc(char *filename) {
- FILE *fp;
- int ch;
- int was_white = 1;
- int words = 0;
- if (!(fp = fopen(filename, "r"))) {
- fprintf(stderr, "%s: can't read\n", filename);
- exitstatus = 1;
- return;
- }
- while ((ch = fgetc(fp)) != EOF) {
- if (isspace(ch)) {
- was_white = 1;
- } else {
- if (was_white)
- words++;
- was_white = 0;
- }
- }
- fclose(fp);
- printf("%d\t%s\n", words, filename);
- }
- int main(int argc, char **argv) {
- int i;
- if (argc < 2) {
- fprintf(stderr, "usage: wc FILE...\n");
- return 1;
- }
- for (i = 1; i < argc; i++)
- wc(argv[i]);
- return exitstatus;
- }
Advertisement
Add Comment
Please, Sign In to add comment