Given a text file, extract words from it. In other words, read the content of file word by word.
Example :
Input: And in that dream, we were flying. Output: And in that dream, we were flying.
Approach :
1) Open the file which contains string. For example, file named “file.txt” contains a string “geeks for geeks”.
2) Create a filestream variable to store file content.
3) Extract and print words from the file stream into a string variable via while loop.
// C++ implementation to read // file word by word #include <bits/stdc++.h> using namespace std; // driver code int main() { // filestream variable file fstream file; string word, t, q, filename; // filename of the file filename = "file.txt"; // opening file file.open(filename.c_str()); // extracting words from the file while (file >> word) { // displaying content cout << word << endl; } return 0; } |
chevron_right
filter_none
Output:
geeks for geeks.
Recommended Posts:
- How to Iterate through a String word by word in C++
- Program to print the Alphabets of a Given Word using * pattern
- Read a record from a File in C++ using seekg() and tellg()
- lseek() in C/C++ to read the alternate nth byte and write it in another file
- Implement your own tail (Read last n lines of a huge file)
- C program to copy contents of one file to another file
- C program to delete a file
- C++ program to create a file
- C Program to find size of a File
- C Program to print contents of file
- C Program to count number of lines in a file
- C++program to delete the content of a Binary File
- C++ program to print unique words in a file
- C++ program to modify the content of a Binary File
- C Program to merge contents of two files into a third file
- C++ program to append content of one text file to another
- Write a C program that displays contents of a given file like 'more' utility in Linux
- Program to delete a line given the line number from a file
- CSV file management using C++
- tellp() in file handling with c++ with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : imim123345667899

