C ++ Reading words from a text file, word by word or char to char

I searched for a book and read my book, trying to write code to read a text file and process words from it one by one, so I can put them in alphabetical order and save the number of words used and a lot of words. I can't seem to get my GetNextWord () function to work correctly, and it drives me crazy.

I need to read the words in the same order and convert each letter to lowercase if it is uppercase. What I know how to do it, and have done it successfully. He just gets the word character by character and puts it on the line that holds me back.

This is my very last attempt: any help would be awesome or a link to a tutorial on how to read word by word from an input file. (The word is the alpha characters az and '(not) ends with a space, comma, period,;,:,, ect ....

void GetNextWord() { string word = ""; char c; while(inFile.get(c)) { while( c > 64 && c < 123 || c == 39) { if((isupper(c))) { c = (tolower(c)); } word = word + c; } outFile << word; } } 
+4
source share
5 answers

Your logic is wrong. The inner loop runs until c changes and there is nothing in it that could change c .

Why do you have two loops? I think you might be confused about whether this function should read the next word or all words. Try to separate these problems, put them in different functions (one of which causes the other). Itโ€™s easiest for me to approach these issues in a descending order:

 while(inFile.good()) { std::string word = GetNextWord(inFile); if(!word.empty()) std::cout << word << std::endl; } 

Now fill in the blanks by specifying GetNextWord() to read everything to the next word boundary.

+3
source

You can read a file in a word using the >> operator. For example, see this link: http://www.daniweb.com/forums/thread30942.html .

I gave their example here:

 ifstream in ( "somefile" ); vector<string> words; string word if ( !in ) return; while ( in>> word ) words.push_back ( word ); 
+8
source

Personally, I like to read on the tab std::getline(std::istream&, std::string&) (in the <string> header, but of course you will also need the #include stream header).

This function breaks into a new line, which is a space for determining your problem. But this is not the whole answer to your question. After reading a line of text, you will need to use string operations or standard algorithms to break the line into words. Or you can iterate over the string manually.

The guts would be something like:

 std::string buffer; while (std::getline(std::cin, buffer) { // break each line into words, according to problem spec } 
0
source

I use

 // str is a string that holds the line of data from ifs- the text file. // str holds the words to be split, res the vector to store them in. while( getline( ifs, str ) ) split(str, res); void split(const string& str, vector<string>& vec) { typedef unsigned int uint; const string::size_type size(str.size()); uint start(0); uint range(0); /* Explanation: * Range - Length of the word to be extracted without spaces. * start - Start of next word. During initialization, starts at space 0. * * Runs until it encounters a ' ', then splits the string with a substr() function, * as well as making sure that all characters are lower-case (without wasting time * to check if they already are, as I feel a char-by-char check for upper-case takes * just as much time as lowering them all anyway. */ for( uint i(0); i < size; ++i ) { if( isspace(str[i]) ) { vec.push_back( toLower(str.substr(start, range + 1)) ); start = i + 1; range = 0; } else ++range; } vec.push_back( toLower(str.substr(start, range)) ); } 

I'm not sure if this is especially useful for you, but I will try. The toLower function is a quick function that simply uses the :: toLower () function. This reads each char to a space, and then fills it with a vector. I'm not quite sure what you mean by char from char.

Do you want to display the word character at a time? Or do you want to test each character as you go? Or do you want you to extract one word, finish, and then come back? If so, I would 1) recommend the vector anyway, and 2) let me know so that I can refactor the code.

0
source

What is needed to complete your inner loop if c == 'a'? The ASCII value for 'a' is 97.

0
source

All Articles