I am very new to c++ , I have started learning c++ using "thinking in c++", and I am practicing the questions. the 3rd question of the 1st chapter is Create a program that opens a file and counts the whitespace-separated words in that file. I could not get the right solution so googled it and found the below solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main() {
ifstream f("helloworld.cpp");
int nwords=0;
string word;
while (f >> word)
++nwords;
cout << "Number of words = " << nwords << endl;
}
what I do not understand is this while (f >> word) , can someone please explain the logic.
operator>> extracts series of non-whitespace characters delimited by whitespace characters.
If it cannot extract next series of characters, it sets failbit in stream it extracts from. It also returns reference to said stream. That sream has implicit conversion operator which converts stram to bool. If everythin is fine with stream (no errors) it converts to true and loop body executer. If not (end of stream) — loop stops.
As "series of non-whitespace characters delimited by whitespace characters" can be defined as "words", it could be said that it counts words.
Example of file content and extracted words:
leading whitespaces are discarded before formatted input operation
We have just successfully read a word. The next character in the stream is thus whitespace. (State A)
The next read operation starts by discarding whitespace until a non-whitespace character is found. We "skip whitespace", skipws. Then the actual "read a word" that stops on next whitespace. We are back at state A.
It is possible to explicitly include the leading whitespace(s) into the word.
Thanks for the reply.
I have written a simple code to understand the same , but now I am trying to modify a little to get the input from user as a string and write the words in the file using the same logic. but it gives me the error
"in function int main
no match for 'operator >>' in 's>> out'"
Please tell me why ?
s is a string. Which does not support steam operations. And you are trying to use stream as right-hand operand to the extraction operator which is not possible too.
You can use stringstreams for that, but:
1) You won't even need loop because there would always be only one word (you can circumvent this by using getline())
2) cout << out << endl; Is clearly wrong. What are you trying to do here?
SO you want:
1) Get a line which consist of several words from user.
2) Output those words, each on its own line, to file.
3) Output those words on screen too.
Do I understand you correctly?
You need to send end of stream signal or go for keskiverto approach and test for sentiel word input.
Or change your code to work with getline() and stringstream:
Also: you are opening same file in two streams. As all IO is buffered, it might not work as you expect. Either close output file before opening it for input, use generic i/o stream or do some synchronization between streams.
Assuming you really want first save words for file and then read them from it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
int main ()
{
std::string s;
cout << "enter the string" << endl;
std::getline(std::cin, s);
std::istringstream input(s);
std::ofstream out("writeme.cpp");
while(input >> s) {
out << s << '\n';
}
out.close();
std::ifstream in("writeme.cpp");
std::cout << in.rdbuf();
}
Yes it does. But eof means that there will no future input in this stream ever. When you enter something in console you will never reach eof normally. You have to manually send EOF to the console which will send it to the stream.
In Unix it is Ctrl+D
In Windows it is Ctrl+Zon new line
Also I told you how to manually send EOF to console to read several lines until no further input is needed.
keskiverto shows example how to take words until sentiel word is entered.
If you want other behavior, describe it and we will help. If you have some trouble with understanding existing example, point it out so we know what do you want.