?
Solved

Readin in a text file.

Posted on 2004-11-10
6
Medium Priority
?
259 Views
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";

      inFile.open(filename.c_str());
      if(inFile.fail())
            cout << "could not open" << endl;                                         //<---- error message.

      while(inFile>>word)
            cout << word << endl;
            system("pause");

      return 0;

}


0
Comment
Question by:travishaberman
  • 5
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12549653
The file name looks incorrect.

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

Accepted Solution

by:
Axter earned 2000 total points
ID: 12549687
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

0
 

Author Comment

by:travishaberman
ID: 12549739
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?

-TH
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

Expert Comment

by:Axter
ID: 12549768
>>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
0
 
LVL 30

Expert Comment

by:Axter
ID: 12549784
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
0
 
LVL 30

Expert Comment

by:Axter
ID: 12549798
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

809 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