Advertisement
Guest User

Untitled

a guest
Sep 18th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.81 KB | None | 0 0
  1. /*******************************************************************************
  2. /
  3. /      filename:  wc.c
  4. /
  5. /   description:  Implements the wc function
  6. /
  7. /        author:  Pelini, Nick
  8. /      login id:  FA_12_CPS444_20
  9. /
  10. /         class:  CPS 456
  11. /    instructor:  Perugini
  12. /    assignment:  Homework #2
  13. /
  14. /      assigned:  September 11, 2012
  15. /           due:  September 18, 2012
  16. /
  17. /******************************************************************************/
  18.  
  19. #include<stdio.h>
  20. #include<stdlib.h>
  21. #include<string.h>
  22.  
  23.  
  24. // how to read in file / multiple files
  25. // check if getopt is correct
  26. // how to check if no file specified then to read stdin
  27. // how / when to print errors
  28. // using argc / argv
  29.  
  30. int main(int argc, char **argv)
  31. {
  32.     char previousLetter = 0;
  33.     char line[1000];
  34.     char fileName[30];
  35.     int length, a, i, numberOfWords, numberOfLines, numberOfChars, numberOfFiles = 0;
  36.     int totalWords = 0;
  37.     int totalLines, totalChars = 0;
  38.     FILE *openedFile;
  39.     extern char *optarg;
  40.     extern int optind;
  41.     int c, err = 0;
  42.     int lFlag=0, wFlag=0, mFlag=0;
  43.     //char *sname = "default_sname", *fname;
  44.     //static char usage[] = "usage: %s [-dmp] -f fname [-s sname] name [name ...]\n";
  45.  
  46.     // Sets flag if user specifies an option
  47.     while ((c = getopt(argc, argv, "l::w::m::")) != -1)
  48.         switch (c) {
  49.         case 'l':
  50.             lFlag = 1;
  51.             break;
  52.         case 'w':
  53.             wFlag = 1;
  54.             break;
  55.         case 'm':
  56.             mFlag = 1;
  57.             break;
  58.         case '?':
  59.             err = 1;
  60.             break;
  61.     }
  62.  
  63.     // use argv and argc to check if filenames were entered
  64.     if (argc > 1)
  65.     {
  66.         a = 1;
  67.         while (a < argc)
  68.         {
  69.             if (argv[a][0] == '-')
  70.             {
  71.             }
  72.             else if (argv[a][0] != '-')
  73.             {
  74.                 numberOfFiles++;
  75.                 totalWords = 0;
  76.                 // Get filename and open file
  77.                 strncpy(fileName, argv[a], 30);
  78.                 openedFile = fopen(fileName, "r");
  79.                 // Read through file and count until EOF reached
  80.                 while (!feof(openedFile))
  81.                 {
  82.                     printf("About to start reading file\n");
  83.                     if (fgets (line, 1000, openedFile) != NULL)
  84.                     {
  85.                         length = strlen(line);
  86.                         if (length > 0)
  87.                         {
  88.                             previousLetter = ' ';
  89.                         }
  90.                         for (i=0; i < length; i++)
  91.                         {
  92.                             if ((!isspace(line[i])) && (isspace(previousLetter)) && (line[i] != EOF))
  93.                             {
  94.                                 numberOfWords++;
  95.                             }
  96.                             numberOfChars++;
  97.                             previousLetter = line[i];
  98.                         }
  99.                         numberOfLines++;
  100.                     }
  101.                 }
  102.                 // Keep total values in case a next file is read
  103.                 totalWords = totalWords + numberOfWords;
  104.                 totalLines = totalLines + numberOfLines;
  105.                 totalChars = totalChars + numberOfChars;
  106.                 numberOfWords = 0;
  107.                 numberOfLines = 0;
  108.                 numberOfChars = 0;
  109.                 // Close the file
  110.                 printf("About to close the file\n");
  111.                 fclose(openedFile);
  112.             }
  113.             // Increment argv count
  114.             a++;
  115.         }
  116.     }
  117.  
  118.     // Read stdin because no files specified
  119.     // Only enter loop if no files
  120.     if (numberOfFiles == 0)
  121.     {
  122.         totalWords = 0;
  123.         printf("Reading stdin\n");
  124.         while (!feof(stdin))
  125.         {
  126.             if (fgets (line, 1000, stdin) != NULL)
  127.             {
  128.                 length = strlen(line);
  129.                 if (length > 0)
  130.                 {
  131.                     previousLetter = ' ';
  132.                 }
  133.                 for (i=0; i < length; i++)
  134.                 {
  135.                     if ((!isspace(line[i])) && (isspace(previousLetter)) && (line[i] != EOF))
  136.                     {
  137.                         totalWords++;
  138.                     }
  139.                     totalChars++;
  140.                     previousLetter = line[i];
  141.                 }
  142.                 totalLines++;
  143.             }
  144.         }
  145.     }
  146.  
  147.     // ****Use field width and precision****
  148.     //  Output totals depending on options inputted
  149.     if (lFlag == 1) {
  150.         printf("%d ", totalLines);
  151.     } else if (wFlag == 1) {   
  152.         printf("%d ", totalWords);
  153.     } else if (mFlag == 1) {   
  154.         printf("%d\n", totalChars);
  155.     } else if (lFlag == 0 && wFlag == 0 && mFlag == 0) {
  156.         printf("%d ", totalLines);
  157.         printf("%d ", totalWords);
  158.         printf("%d\n", totalChars);
  159.     } else if (err) {
  160.         fprintf(stderr, "invalid option -- ", argv[0]);
  161.         exit(1);
  162.     }
  163.  
  164.     exit(0);
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement