By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,377 Members | 3,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,377 IT Pros & Developers. It's quick & easy.

Why is ifstream not opening my file?

P: n/a
This is my code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <string>
  6. using namespace std;
  8. int main()
  9. {
  10.     ifstream infile;
  13.     if (!infile.is_open())
  14.     {
  15.         return 1; //if cannot find file, all of the time
  16.     }
  18.     char response;
  19.     cin >> response;
  21.     return 0;
  22. }
The program cannot open the file, and thus always returns 1;

My text1.txt file is in C:\Documents and Settings\Alex\My Documents\Visual Studio 2010\Projects\frequency\frequency\text1.txt

How do I make it open the file so 1 is not returned?
Oct 3 '10 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 8,916
Your program attempts to open a file in the current directory so the most likely reason is that your program is not being run in the same directory as the directory you have placed your text1.txt file in.
Oct 3 '10 #2

P: 29
I have figured out why:

When I write
Expand|Select|Wrap|Line Numbers
I need to write the directory as so:


Thank you.
Oct 4 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.