Let's assume that you have the following file that need to be parsed:
My name is Milto and I like Capoeira! :)
Then the following code will read the file and print each word at separated line.
#include < iostream > #include < fstream > #include < iterator > #include < string > #include < vector > int main(void) { std::string filename = "inputt.xt"; std::ifstream mystream(filename.c_str()); if(!mystream) { std::cerr << "File \"" << filename << "\" not found.\n"; } std::istream_iterator< std::string > it(mystream); std::istream_iterator< std::string > sentinel; std::vector < std::string > words(it,sentinel); for(unsigned int i=0; i< words.size();++i) { std::cout << words[i] << "\n"; } return 0; }
The output result will be the following:
$: g++ parsing.cpp -o out $: ./out My name is Milto and I like Capoeira! :)
You may download this example code from here:
https://www.dropbox.com/s/2chkdvjf2m0uhcp/parseFileIntoWords.zip?dl=0
No comments:
Post a Comment