ifstream can't open file

M

Murasama

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;

}
 
G

Gang Ji

Try to copy your file from debug directory to the parent directory of
debug (where the project file lives).

Best,
Gang
 
M

Murasama

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.
 
K

Kai-Uwe Bux

Murasama said:
#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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top