Question
Why I'm getting this error: Segmentation fault When I test my progam with mywords -c -f the testfile mywords -s testfile #include #include #include #include
Why I'm getting this error: "Segmentation fault"
When I test my progam with mywords -c -f the testfile
mywords -s testfile #include
* A promgram to read a text file and print it to the * display. It optionally find the count of the number * of words in the file, and/or find the number of * occurrences of a substring, and/or take all the words * in the string and sort them lexicographically * (ASCII order). User input is not needed. */ //Declare constants #define n 10000 #define OUT 0 #define IN 1 // Method countWords() to return the number of words in the file unsigned countWords(char *word){ int state = OUT; unsigned wordCount = 0; while (*word){ // Checking if a next character is not a word separator if (*word == ' ' || *word == ' ' || *word == '\t'){ state = OUT; } else if (state == OUT){ // Setting state as IN state = IN; // Incrementing wordCount ++wordCount; } // Moving to the next character ++word; } return wordCount; } // Method sortWords() to print the words in the file sorted by ASCII order void sortWords(char *str1, int numWords){ //Declaring local variables int i, j, c = 0; char temp[50], *splitString, str[60][20]; splitString = strtok(str1, " ,.-"); // While loop to check the string until null while (splitString != NULL){ strcpy(str[c], splitString); splitString = strtok(NULL, " ,.-"); c++; } for (i = 1; i < numWords; i++){ for (j = 1; j <= numWords - i; j++){ if (strcmp(str[j - 1], str[j]) > 0){ strcpy(temp, str[j - 1]); strcpy(str[j - 1], str[j]); strcpy(str[j], temp); } } } // Sorting the words printf(" Sorted Order: "); for (i = 0; i < numWords; ++i){ puts(str[i]); } printf(" "); } // Method occurrenceSearch to find occurence of string void occurrenceSearch(char *word, char *findStr){ // Declaring variables to count occurences and get length of string int s = 0, l = 0, occurences= 0, length; length = strlen(word); while (findStr[s] != '\0'){ if (findStr[s] == word[l]){ while (findStr[s] == word[l] && findStr[s] != '\0'){ s++; l++; } if (l == length && (findStr[n] == '\0' || findStr[s] == '\0')){ occurences++; } } else { while (findStr[s] != ' '){ s++; if (findStr[s] == '\0') continue; } } s++; l = 0; } // Printing the number of occurences if (occurences > 0){ printf("Substring '%s' was found%d time(s) in the file. ", word, occurences); } else{ printf("Substring '%s' does not appear in the file. ", word); } } //main method int main(int argc, char *argv[]){ int option; // To flag options and errors int cFlag = 0; int sFlag = 0; int fFlag = 0; int error = 0; // Usage static char usage[] = "mywords [-cs] [-f substring] filename "; // Declaring variables to read file char buff[n]; FILE *file; size_t nread; int numOfWords; // Opening file file = fopen(argv[argc-1], "r"); // If file exists if (file){ // Printing file content printf(" File Content: "); while ((nread = fread(buff, 1, sizeof buff, file)) > 0) fwrite(buff, 1, nread, stdout); if (ferror(file)){ printf("Error! The file does not exist."); } // Closing the file fclose(file); } // getopt function to read commands while ((option = getopt(argc, argv, ":-f:cs")) != -1){ switch (option){ // If option = c, set cFlag = 1 case 'c': cFlag = 1; break; // If option = f, set fFlag = 1 case 'f': fFlag = 1; break; // If option = s, set sFlag = 1 case 's': sFlag = 1; break; // If there is an unknown option flag error case '?': error = 1; break; } } // Checking if file path was provided if (optind >= argc){ fprintf(stderr, "%s: Please, provide file path! ", argv[0]); fprintf(stderr, usage, argv[0]); exit (1); } // If error = 1, displays usage else if (error){ printf("Unknown option! "); fprintf(stderr, usage, argv[0]); exit (1); } // If option = c, call the method to count number of words in the file if (cFlag){ numOfWords = countWords(buff); printf(" Number of words: %d ", numOfWords); } // If option = f call the method to find number of occurrences of the given substring if (fFlag){ occurrenceSearch(optarg, buff); } // If option = s call the method to sort words if (sFlag){ sortWords(buff, numOfWords); } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started