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

ifstream can't open file

P: n/a
Hi there,

Im trying a simple file IO operation in Visual Studio .NET 2003 and it
can't seem to open the file.
If I run the exe in the debug directory it works fine but if I click
the start button (blue arrow) then it fails to open the file.

any ideas?

source:

#include <iostream>
#include <fstream>

using namespace std;
int main(){

ifstream in;
in.open("test.txt");
char str[80];

if(in.is_open()){
cout << "open ok" << endl;
in >> str;
cout << str;
cout << "printed?" << endl;
}
else
cout << "could not open file" << endl;

for(;;);

return 0;

}

Sep 30 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Try to copy your file from debug directory to the parent directory of
debug (where the project file lives).

Best,
Gang

Murasama wrote:
Hi there,

Im trying a simple file IO operation in Visual Studio .NET 2003 and it
can't seem to open the file.
If I run the exe in the debug directory it works fine but if I click
the start button (blue arrow) then it fails to open the file.

any ideas?

source:

#include <iostream>
#include <fstream>

using namespace std;
int main(){

ifstream in;
in.open("test.txt");
char str[80];

if(in.is_open()){
cout << "open ok" << endl;
in >> str;
cout << str;
cout << "printed?" << endl;
}
else
cout << "could not open file" << endl;

for(;;);

return 0;

}

Sep 30 '05 #2

P: n/a
Thanks a lot, that worked.

As soon as I read your post I immediately remembered about the "working
directory" settings in the project properties.

Thanks again.

Sep 30 '05 #3

P: n/a
Murasama wrote:
#include <iostream>
#include <fstream>

using namespace std;
int main(){

ifstream in;
in.open("test.txt");
char str[80];

if(in.is_open()){
cout << "open ok" << endl;
in >> str;
cout << str;
cout << "printed?" << endl;
}
else
cout << "could not open file" << endl;

for(;;);

return 0;

}


Did you make sure that there is a file "test.txt"?
Best

Kai-Uwe Bux
Sep 30 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.