Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have a text which contains a lot of words, For example i want to find and print a word "Crow" from text file....which function should i use to find and print on screen using file handling in c++
Posted
Updated 23-May-14 5:54am
v2
Comments
[no name] 23-May-14 11:05am    
There must be thousands and thousands of examples for creating and reading from a text file in C++ out there. Have you done any research at all into your problem (whatever it actually is)?
OriginalGriff 23-May-14 11:07am    
Of course he has!
He found here, didn't he? :laugh:
[no name] 23-May-14 14:19pm    
I wonder if you google "do my homework for me" if the first hit is CP....
OriginalGriff 23-May-14 14:30pm    
But a simple mod to the search string found this on the fourth try...:sigh:
http://www.codeproject.com/Questions/533628/java-plus-plus-plus
[no name] 23-May-14 14:23pm    
It's "no" btw. CP isn't even on the first page.

C++
// reading a text file
#include < iostream > 
#include < fstream >
#include <string >
using namespace std;
 
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
 
else cout << "Unable to open file"; 
 
return 0;
}
 
Share this answer
 
v3
Comments
ihsanrai 23-May-14 12:02pm    
this is c# i want in c++
salemAlbadawi 23-May-14 12:38pm    
Reading from a file can also be performed in the same way that we did with cin:


// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}

else cout << "Unable to open file";

return 0;
}
CPallini 23-May-14 13:00pm    
That's C++.
[no name] 23-May-14 13:02pm    
It is now, wasn't before he edited his posting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900