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;
}
NetVipeC
  • 4,402
  • 1
  • 17
  • 19
  • It's not clear to me what you are asking us to help you with. – R Sahu Sep 16 '14 at 17:01
  • My apologies; I am basically trying to count certain characters in the file. I can't seem to do it with strtok(). Is there a way to count sought characters with getline? in the loop: while(infile.getline(input, INPUT_SIZE)) – Black and yellow Sep 16 '14 at 17:05
  • you have some problems with `}` at the end of the code. – NetVipeC Sep 16 '14 at 17:06
  • just change position of } and it works fine. : https://ideone.com/YbQZvq – uchar Sep 16 '14 at 17:30

0 Answers0