Hello everybody,
My goal with this programm is to implement a function that reads a file with text, and returns successively single words, that can be handled by another function.
One of the problems is, that for some reason it stops giving outputs at the last word and stays running instead of breaking up ( return 0; ).
I wonder also, how could/should I implement this as a method, that throws all the words of the file, so that they can be handled by another method?
Thanks in advance!
Code:#include <iostream> #include <fstream> #include <string> int main() { char ch; std::string word = ""; // Givin the InputFile a more suitable name. std::fstream inputFile("test.txt"); // pointer initialisation, pointing to first character FILE *fptr = fopen("test.txt", "r"); // ch has the first character of the file ch = getc( fptr ); // Read till end of file (problem: it doesnt stop after printing the last word) while(!inputFile.eof()) { if ( (ch != ' ') && (ch != '\n') ) { word += ch; ch = getc( fptr ); // Reading ' ' or '\n' implies the end of a word. print it. if( (ch == ' ') || (ch == '\n') ) { std::cout << word << std::endl; word = ""; } } if( (ch == ' ') || (ch == '\n')) { ch = getc( fptr ); } if( !((ch != ' ') || (ch == ' ')) ) { ch = getc( fptr ); } } return 0; }