Hi I'm new to computer programming. 'Im just needing to learn some basic c++ for a larger project. So if someone wouldn't mind guiding me through some basics on i/o streams. I need to able to open a .txt file saved on my c drive and be able to count certain words in the file. So far Im just trying to figure out how to open a text file.
This is what I have:

// 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 (! myfile.eof() )
{
cout << myfile << endl;
}
myfile.close();
}

else cout << "Unable to open file"; 

return 0;
}

however, when I run it "0x22fec4" continues to display constantly until I terminate it.

any ideas? Thanks.

You open the file. In the loop you continually print out the stream, but you never read from the file and then never reach the end of it.

the problem is that you are displaying the value of the ifstream reference. your while loop is good but you need to store the data into some other variable and then display it via cout. such as:

//...
while (!myfile.eof())
{
       myfile.getline(line, "\n");
       cout << line << "\n";
}
//...

i used getline here so if there are spaces in the line of text it ill keep reading until the end of the line. hope this helps you out.

i saw your link just a few minutes ago Dave and it is a very good idea. i had always wondered why it didn't work right sometimes and this really helped.

Another good way of doing it (without use eof() ) is:

while (myFile.getline(line, '\n'))
      cout << line << endl;
#include <fstream>
ofstream text;
string name;
text.open("myfile.txt");

would work as well.

also if you know the syntax for cout using the operators << and >> then you can output to file like so using my above example

text<<"Hello World";
//or if you wanted to use a variable
getline(cin,name);
text<<name;

im still learning how to take input from files however.
hope this helps :P

the problem is that you are displaying the value of the ifstream reference. your while loop is good but you need to store the data into some other variable and then display it via cout. such as:

//...
while (!myfile.eof())
{
       myfile.getline(line, "\n");
       cout << line << "\n";
}
//...

i used getline here so if there are spaces in the line of text it ill keep reading until the end of the line. hope this helps you out.

NathanOliver, I tried your example and I got the following error:

no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::getline(std::string&, const char[38])'

I have no idea what I am doing wrong

don't worry about using eof() its not important unless your wanting to or your instructor has told you to or unless you define the end of the file. use another variable. if you want the loop to exit simply add a way to exit here is an example:

int x;
x=0;
do{
cout<<"enter a number for x:";
cin>>x;
}
// or other code.
while(x<100);//while x is less than 100.
//it will continue to loop until the user enters 100 or anything higher.

Loops at cplusplus.com
so try another variable end of file isnt nessary.
EDIT: actually using a do while loop would be a lot better for that.

int x;
x=0;
do{
//you could use an if statement to exit if needed;
myfile.getline(line, "\n");
cout << line << "\n";
}
while(x<1);

EDIT2:disregard my first post in this thread. :P

NathanOliver, I tried your example and I got the following error:

no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::getline(std::string&, const char[38])'

I have no idea what I am doing wrong

#include <fstream>
#include <string>
using namespace std;

int main ()
{
   string line;
   ifstream file ("file.txt");
   while ( getline(file, line) )
   {
      cout << line << endl;
   }
   return 0;
}

Dave, thanks that solved my problem. Thanks everyone for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.