Q:

Cannot read text file in C++

I am stumped by this simple problem. I am reading a text file with C++: 
std::ifstream stream; 
stream.open(filename); 
if (!stream) 
   cout << "Invalid stream" << endl; 

And !stream is true but there seems to be nothing wrong with the text file. Under what circumstances can stream be false? 
Note: is_open returns true 

A:

You have not provided enough information. Nevertheless, my psychic powers reveal:
filename is a relative path, and your current working directory is not what you think it is.
Inside your if clause, before printing via  std::cout, add this:
perror(filename.c_str());

A:

This code works for me:
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int main()

{
    std::ifstream infilestream;
    std::string line;

    infilestream.open("test.txt");
    while(infilestream)
    {
      std::getline(infilestream, line);
      cout<<line<<"\n";
    }
    infilestream.close();

    return(0);

}

