Readin in a text file.

Posted on 2004-11-10
Last Modified: 2010-04-01

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;


Question by:travishaberman
    LVL 30

    Expert Comment

    The file name looks incorrect.

    Try the following:
    string filename = "textdocument.txt";
    LVL 30

    Accepted Solution

    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


    Author Comment

    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?

    LVL 30

    Expert Comment

    >>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
    LVL 30

    Expert Comment

    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
    LVL 30

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now