READING FROM FILE WORD BY WORD !

Hello everyone ... I am really beginner in c++ and i need help in this
My question is : How can i complete this code below to read WORD BY WORD from input.txt ?

1
2
3
4
5
6
7
8
9
10
11
12
13
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
    ifstream file("input.txt");
    string word;
    if (file.is_open()){
    <<<<<<<<<< complete here >>>>>>>>>>>
    }
    return 0;
}


input.txt :
I love c++
I love c++ so much
I need help




Hello MohammadAlshareef,

The code below should give you a good starting point.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
#include <thread>

//using namespace std;// <--- Best not to use.

int main()
{
	std::ifstream file("input.txt");
	std::string word;

	if (!file)
	{
		std::cout << "\n  File did not open" << std::endl;
		// <--- This may or may not be needed
		std::this_thread::sleep_for(std::chrono::seconds(5));  // Requires header files "chrono" and "thread".
		return 1;
	}
	while (file >> word)
	{
		std::cout << ' ' << word << std::endl;
		// <--- Store the words in a vector for later use or do other processing on them.
	}

	return 0;
}

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	std::ifstream file("input.txt");
	std::string word;

	if (!file)
	{
		std::cout << "\n  File did not open" << std::endl;
		return 1;
	}
	while (file >> word)
	{
		std::cout << ' ' << word << std::endl;
	}

	return 0;
}


the code which you gave me didn't work but this part of it worked perfectly ?
but please explain this part :
1
2
3
4
while (file >> word)
	{
		std::cout << ' ' << word << std::endl;
	}

and tell how can i read and cout just ONE WORD ?
And thank you !
Last edited on
Stop using all caps and exclamation points, ahole.
Hello MohammadAlshareef,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Based on what you posted I would say that you did not copy the code properly because it does work. If something gave you an error then say what it is.

I should have mentioned this for the line
std::this_thread::sleep_for(std::chrono::seconds(5)); // Requires header files "chrono" and "thread". You need the header files mentioned and the only part to worry about is the number in (). This is the number of whole seconds that the program will pause before continuing. Some times you do not have to know everything just how to use it.

If you want to count the words define a variable like int count{};. The empty {}s will initialize the variable to zero. Then in the while loop add count++;. After the while loop you can "cout" "count" to see how many words there are.

Hope that helps,

Andy
Hello MohammadAlshareef,

Sorry I missed this the first read through and I wanted to think about it during dinner.

1
2
3
4
while (file >> word)
{
    std::cout << ' ' << word << std::endl;
}


People who are new to the language either read about or are taught in school about "eof", (or End Of File). This may seem like the best way to write the while condition, but they end up here or somewhere else asking why the last bit of information is a duplicate. This is because by the time the while condition figures out it is at "eof" the last read is processed twice before the while loop ends.

One way to solve this problem is to do the read in the while condition. As long as there is something to read the condition is true and when it tries to read past "eof" the stream fails thus making the while condition false and execution of the program falls to the next line after the while loop. This is the best way to read a file of unknown length. The way you set up the read in the while condition will depend on what you have to read.

The file >> word part will read up to the first white space or new line character whichever comes first. Since the words in your input file are separated by a space and end with a new line this works well to read each word.

The above code is just a simple read and write. Put what you need in the {}s of the while loop.

My apologies you wrote "cout" and I thought I read "count", so part of my previous response may seem a bit confusing. Just discard it if you do not need it.

To read and "cout" one word all you would need is:
1
2
3
file >> word;

std::cout << ' ' << word << std::endl;

And if you need another word from the file just do it again, but after the first read you will need to check for "eof" before the next read with an if statement to know if it is safe to read.

Sorry for the confusion, but when your original code leaves a lot of guess work it is easy to guess wrong.

Hope this clears things up,

Andy
Thnaks you so much ... that really helped me ^_^
Topic archived. No new replies allowed.