C++ programming tokenizing and adding tokens to vector

dhrfrenfren94
timer Asked: Dec 5th, 2015

Question Description

.cpp file


#include <cstdlib>

#include <iostream>

#include <iomanip>

#include <cstring>

#include <string>

#include <cstdio>

#include <sstream>

#include <vector>

usingnamespace std;

#include "myfilereader.h"

MyFileReader::MyFileReader(constchar *filename)

{

        if (filename != NULL) {

                fp = fopen(filename, "r");

                lineno = 1;

                if (fp == NULL){

                        perror(filename);

                        exit(1);

                }

        }

}

MyFileReader::~MyFileReader()

{

        fclose(fp);

}

int MyFileReader::getLine(char* buffer, int size)

{

        cout << "check entering get line" << endl;

        char *check = fgets(buffer,size, fp);

        int length = strlen(buffer);

        cout << "checking the length of the buffer" << length << endl;

        //if fgets returns null, could either be at the end of file or error

        if(check==NULL){

                cout << "the check is NULL so nothing in the buffer" << endl;

                if(feof(fp)!= 0){

                        cout << "reached EOF from get line" << endl;

                        return -1;

                }

                else {

                        cout << "reached end of line" << endl;

                        return0;

                }

        }

        elseif (buffer[length-1]=='\n'){

                cout << "the check is not NULL" << endl;

                //buffer[length-1]== '\n'

                lineno = lineno+1;

                cout << "this is the lineno: " << lineno << endl;

                return1;

        }

        else{

                return0;

        }

}

bool MyFileReader::haveAllLinesBeenRead() const

{

        int result;

        result = feof(fp);

        //if the result is equal to 0, not EOF

        if (result == 0){

                cout << "check check check" << endl;

                returntrue;

        }

        else{

                cout << "have all lines been read is false" << endl;

                returnfalse;

        }

}


void MyFileReader::tokenizeLine(vector<string>& vec)

{

        char buf[100];

        int result =getLine(buf, sizeof(buf));

//      while(result != 0){

        string line("");

        while(1){

//while 1: reads in the first line

//while 2: tokenizes the string afer being read in

                if (result == -1){

                        int lineno = getCurrentLineNumber() -1;

                        cout << "[" << setw(4) << lineno << "]";

                        break;

                }

                elseif (result == 1) {

                        cout << "in tokenize, result is not -1" << endl;

                        string str(buf);

                        istringstream iss(str);

                        string hold;

                        cout << "this is the original string buf: " << str << endl;

                        unsignedint token = 0;

                        while (iss >> hold){

                                char c;

                                string hold2;

                                unsignedint i;

                                for (i=0;i< hold.size();i++){

                                        if(isalpha(hold[i])){

                                                c = tolower(hold[i]);

                                                hold2 += c;

                                                }

                                        }

                                        cout << "this is what is in hold2" << hold2 << endl;

                                        vec.push_back(hold2);

                                        token = token + 1;

                                        cout << "this is the number of tokens: " << token << endl;

                        }

                        int lineno = getCurrentLineNumber()-1;

                        cout << "[" << setw(4) << lineno << "]";

                        cout << "{ " << setw(1) << token << " tokens}";

                        line = "";

                        cout << "gets to the end of the tokenizing function" << endl;

                }

                else {

                        line+=buf;

                }

                exit(1);

        }

                unsigned i = 0;

                for(vector<string>::iterator it = vec.begin(); it != vec.end();i++){

                        cout << *it << endl;

                }

}

main function:

#include <cstdlib>

#include <iostream>

#include <iomanip>

#include <string>

#include <vector>

usingnamespace std;

#include "myfilereader.h"

int main(int argc, char **argv)

{

        if (argc != 2) {

                cerr << "usage: ncat <file_name>" << endl;

                exit(1);

        }

        constchar *filename = argv[1];

        MyFileReader f(filename);

        while(1){

        //while (f.haveAllLinesBeenRead()){

                while(f.haveAllLinesBeenRead()){

                        vector<string> vec;

                        cout << "check" << endl;

                        f.tokenizeLine(vec);

                        cout << "check2" << endl;

                        for(vector<string>::iterator it = vec.begin(); it != vec.end(); ++it){

                                cout << "is this printing?" << endl;

                                cout << *it << "is this printing" << endl;

                        }

                }

        }

        return0;

}

header file:

#ifndef _MYFILEREADER_H__

#define __MYFILEREADER_H__

#include <iostream>

#include <cstring>

usingnamespace std;

class MyFileReader {

    public:

        // default constructor

        MyFileReader();

        // constructor

        MyFileReader(constchar* p);

        // destructor

        ~MyFileReader();

        //declaration of MyFileReader::getLine

        int getLine(char *buffer, int size);

        int getCurrentLineNumber(){ return lineno;}

        void tokenizeLine(vector<string>& vec);

        bool haveAllLinesBeenRead() const;

    private:

        FILE *fp;

        int lineno;

        MyFileReader(const MyFileReader&);

        MyFileReader& operator=(const MyFileReader&);

};

#endif


 

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

This question has not been answered.

Create a free account to get help with this and any other question!

Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors