Readin in a text file.

I am beginning a simple program that needs to read in a text file as its input.  But the file will not open correctly.   Is there any glaring mistake in my code?... I always get the "error message."

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main(){

      ifstream inFile;
      string word;
      string filename = "/textdocument.txt";;
            cout << "could not open" << endl;                                         //<---- error message.

            cout << word << endl;

      return 0;


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The file name looks incorrect.

Try the following:
string filename = "textdocument.txt";
I recommend you first test it out with a full path name, and then try a relative path.

If you're programming in Windows/Dos/OS2, then a full path would look something like the following:
string filename = "c:\\windows\\system32\\textdocument.txt";

Notice that there is two \\ characters to represent every single backslash character.

In UNIX/Linux the full path would look something like this:
string filename = "/home/username/textdocument.txt";

Notice the use of forward slash instead of backslash.

If you need a relative path, then it will look like this:

string filename = "../somedir/textdocument.txt";  //For UNIX/Linux

string filename = "..\\somedir\\textdocument.txt";  //For Windows/DOS


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
travishabermanAuthor Commented:
Hmm... that could be my mistake.. if you are right then my path should look like this right?

      string filename = "C:\\Documents and Settings\\Travis Haberman\\Desktop\Project 4\\hashing\\textdocument.txt";

I try that and it does not work though.  Any more thoughts?  Does it matter what my text document has as a first character?

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

>>string filename = "C:\\Documents and Settings\\Travis Haberman\\Desktop\Project 4\\hashing\\textdocument.txt";

You're string still has a single backslash right before Project 4
Try the following:
string filename = "C:\\Documents and Settings\\Travis Haberman\\Desktop\\Project 4\\hashing\\textdocument.txt";

Also, make sure the following is a valid path:
C:\Documents and Settings\Travis Haberman\Desktop\Project 4\hashing
If your program is launched in the hashing subdirectory, then you can just use teh file name by itself.
string filename = "textdocument.txt";

Use above method if target file is in current working directory.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.