0

I am preparing a simple program that is supposed to take a file and then check for instances of certain characters ( spaces, some sentence delimiters and so on ).

In the code below, I have the file open. I was able to get delimiters working by using strtok, but then I was not able to count them properly. It didn't work well when I tried using it for counting other things ( syllables, spaces ). In other words, not good for what I am trying to do ( unless there is a way ).

I am stuck. I know I have to use getline() to parse the file, but I can't seem to make it happen. I just a need push in the right direction.

#include <iostream>
#include <cstring>
#include <fstream>

using namespace std;

int main(int argc, char* argv[]) {
    int space_count = 0;
    int sentence_count = 0;
    int word_count = 0;
    int syllable_count = 0;
    int enter_count = 0;
    string syll = "aeiou";

    char delim[] = " ";
    char* token;
    char const* const fileName = argv[1];

    // check we have at least 2 command line
    // arguments
    // that argv[1] is valid
    if (2 > argc) {
        cout << "Correct usage <file> path>" << endl;
        return 0;
    }

    ifstream infile;
    const int INPUT_SIZE = 10000;
    char input[INPUT_SIZE];

    infile.open(argv[1]);
    if (!infile.is_open()) {
        cout << "could not open file!" << endl;
        return 0;
    }

    while (infile.getline(input, INPUT_SIZE)) {
        // check input to see if it matches given char or a string
        //
        //    if (input[INPUT_SIZE] = 'a'){
        space_count++;
    }

    // word count by using spaces set in delim
    token = strtok(input, delim);
    cout << token << endl;

    word_count++;

    // file print
    while ((token = strtok(NULL, delim)) != NULL) {
        cout << token << endl;
        word_count++;
    }
}

cout << word_count << endl;

return 0;
}
4

0 回答 0