Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 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, totalLines, totalChars = 0;
  37. FILE *openedFile;
  38. extern char *optarg;
  39. extern int optind;
  40. int c, err = 0;
  41. int lFlag=0, wFlag=0, mFlag=0;
  42. //char *sname = "default_sname", *fname;
  43. //static char usage[] = "usage: %s [-dmp] -f fname [-s sname] name [name ...]\n";
  44.  
  45. // Sets flag if user specifies an option
  46. while ((c = getopt(argc, argv, "l::w::m::")) != -1)
  47. switch (c) {
  48. case 'l':
  49. lFlag = 1;
  50. break;
  51. case 'w':
  52. wFlag = 1;
  53. break;
  54. case 'm':
  55. mFlag = 1;
  56. break;
  57. case '?':
  58. err = 1;
  59. break;
  60. }
  61.  
  62. // use argv and argc to check if filenames were entered
  63. if (argc > 1)
  64. {
  65. a = 1;
  66. while (a < argc)
  67. {
  68. if (argv[a][0] == '-')
  69. {
  70.  
  71. a++;
  72. }
  73. else
  74. {
  75. numberOfFiles++;
  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. if (fgets (line, 1000, openedFile) != NULL)
  83. {
  84. length = strlen(line);
  85. if (length > 0)
  86. {
  87. previousLetter = line[0];
  88. }
  89. for (i=0; i < (length - 1); i++)
  90. {
  91. if (!isspace(line[i]) && isspace(previousLetter) && line[i] != EOF)
  92. {
  93. numberOfWords++;
  94. }
  95. numberOfChars++;
  96. previousLetter = line[i];
  97. }
  98. numberOfChars++;
  99. numberOfLines++;
  100. }
  101. }
  102. }
  103. // Keep tota1l values in case a next file is read
  104. totalWords = totalWords + numberOfWords;
  105. totalLines = totalLines + numberOfLines;
  106. totalChars = totalChars + numberOfChars;
  107. // Close the file
  108. fclose(openedFile);
  109. // Increment argv count
  110. a++;
  111. }
  112. }
  113.  
  114. // Read stdin because no files specified
  115. // Only enter loop if no files
  116. if (numberOfFiles == 0)
  117. {
  118. while (!feof(stdin))
  119. {
  120. if (fgets (line, 1000, stdin) != NULL)
  121. {
  122. length = strlen(line);
  123. if (length > 0)
  124. {
  125. previousLetter = line[0];
  126. }
  127. for (i=0; i < (length - 1); i++)
  128. {
  129. if (!isspace(line[i]) && isspace(previousLetter) && line[i] != EOF)
  130. {
  131. totalWords++;
  132. }
  133. totalChars++;
  134. previousLetter = line[i];
  135. }
  136. totalChars++;
  137. totalLines++;
  138. }
  139. }
  140. }
  141.  
  142. // ****Use field width and precision****
  143. // Output totals depending on options inputted
  144. if (lFlag == 1) {
  145. printf("%d ", numberOfLines);
  146. } else if (wFlag == 1) {
  147. printf("%d ", numberOfWords);
  148. } else if (mFlag == 1) {
  149. printf("%d/n", numberOfChars);
  150. } else if (lFlag == 0 && wFlag == 0 && mFlag == 0) {
  151. printf("%d ", totalLines);
  152. printf("%d ", totalWords);
  153. printf("%d\n", totalChars);
  154. } else if (err) {
  155. fprintf(stderr, "invalid option -- ", argv[0]);
  156. exit(1);
  157. }
  158.  
  159. exit(0);
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement