Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************************
- /
- / filename: wc.c
- /
- / description: Implements the wc function
- /
- / author: Pelini, Nick
- / login id: FA_12_CPS444_20
- /
- / class: CPS 456
- / instructor: Perugini
- / assignment: Homework #2
- /
- / assigned: September 11, 2012
- / due: September 18, 2012
- /
- /******************************************************************************/
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- // how to read in file / multiple files
- // check if getopt is correct
- // how to check if no file specified then to read stdin
- // how / when to print errors
- // using argc / argv
- int main(int argc, char **argv)
- {
- char previousLetter = 0;
- char line[1000];
- char fileName[30];
- int length, a, i, numberOfWords, numberOfLines, numberOfChars, numberOfFiles = 0;
- int totalWords, totalLines, totalChars = 0;
- FILE *openedFile;
- extern char *optarg;
- extern int optind;
- int c, err = 0;
- int lFlag=0, wFlag=0, mFlag=0;
- //char *sname = "default_sname", *fname;
- //static char usage[] = "usage: %s [-dmp] -f fname [-s sname] name [name ...]\n";
- // Sets flag if user specifies an option
- while ((c = getopt(argc, argv, "l::w::m::")) != -1)
- switch (c) {
- case 'l':
- lFlag = 1;
- break;
- case 'w':
- wFlag = 1;
- break;
- case 'm':
- mFlag = 1;
- break;
- case '?':
- err = 1;
- break;
- }
- // use argv and argc to check if filenames were entered
- if (argc > 1)
- {
- a = 1;
- while (a < argc)
- {
- if (argv[a][0] == '-')
- {
- a++;
- }
- else
- {
- numberOfFiles++;
- // Get filename and open file
- strncpy(fileName, argv[a], 30);
- openedFile = fopen(fileName, "r");
- // Read through file and count until EOF reached
- while (!feof(openedFile))
- {
- if (fgets (line, 1000, openedFile) != NULL)
- {
- length = strlen(line);
- if (length > 0)
- {
- previousLetter = line[0];
- }
- for (i=0; i < (length - 1); i++)
- {
- if (!isspace(line[i]) && isspace(previousLetter) && line[i] != EOF)
- {
- numberOfWords++;
- }
- numberOfChars++;
- previousLetter = line[i];
- }
- numberOfChars++;
- numberOfLines++;
- }
- }
- }
- // Keep tota1l values in case a next file is read
- totalWords = totalWords + numberOfWords;
- totalLines = totalLines + numberOfLines;
- totalChars = totalChars + numberOfChars;
- // Close the file
- fclose(openedFile);
- // Increment argv count
- a++;
- }
- }
- // Read stdin because no files specified
- // Only enter loop if no files
- if (numberOfFiles == 0)
- {
- while (!feof(stdin))
- {
- if (fgets (line, 1000, stdin) != NULL)
- {
- length = strlen(line);
- if (length > 0)
- {
- previousLetter = line[0];
- }
- for (i=0; i < (length - 1); i++)
- {
- if (!isspace(line[i]) && isspace(previousLetter) && line[i] != EOF)
- {
- totalWords++;
- }
- totalChars++;
- previousLetter = line[i];
- }
- totalChars++;
- totalLines++;
- }
- }
- }
- // ****Use field width and precision****
- // Output totals depending on options inputted
- if (lFlag == 1) {
- printf("%d ", numberOfLines);
- } else if (wFlag == 1) {
- printf("%d ", numberOfWords);
- } else if (mFlag == 1) {
- printf("%d/n", numberOfChars);
- } else if (lFlag == 0 && wFlag == 0 && mFlag == 0) {
- printf("%d ", totalLines);
- printf("%d ", totalWords);
- printf("%d\n", totalChars);
- } else if (err) {
- fprintf(stderr, "invalid option -- ", argv[0]);
- exit(1);
- }
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement