Unable to open file for reading using C++ I/O [modified]
-
Hi I am trying to open a file for reading using C++ I/O. The call to read is simple #include using namspace std; int _tmain() { std::string infile(_T("testreport.txt")); ifstream fin(infile.c_str(), ios::in); if(!fin) { cout << "unable to open file for reading"; } return 0; } when I execute the program I always get the failure. However if I specify the full path to the file like "d:\\testdata\\debug\\testreport.txt", I am able to open the file successfully. I am using VC++ 2005 compiler and debugging with in VC IDE. How do I make VC++ to automatically take the relative path of the file. This problem only occurs when I am debugging through the VC++ IDE. Thanks and Regards
modified on Thursday, March 27, 2008 5:49 AM
-
Hi I am trying to open a file for reading using C++ I/O. The call to read is simple #include using namspace std; int _tmain() { std::string infile(_T("testreport.txt")); ifstream fin(infile.c_str(), ios::in); if(!fin) { cout << "unable to open file for reading"; } return 0; } when I execute the program I always get the failure. However if I specify the full path to the file like "d:\\testdata\\debug\\testreport.txt", I am able to open the file successfully. I am using VC++ 2005 compiler and debugging with in VC IDE. How do I make VC++ to automatically take the relative path of the file. This problem only occurs when I am debugging through the VC++ IDE. Thanks and Regards
modified on Thursday, March 27, 2008 5:49 AM
psychedelic_fur wrote:
How do I make VC++ to automatically take the relative path of the file.
First it has nothing to do with Visual C++. You are using a library called STL to open the file. It uses the OS support to open the file and they both support relative paths. But just like an absolute path you have to get the relative path correct or it won't work.
led mike
-
Hi I am trying to open a file for reading using C++ I/O. The call to read is simple #include using namspace std; int _tmain() { std::string infile(_T("testreport.txt")); ifstream fin(infile.c_str(), ios::in); if(!fin) { cout << "unable to open file for reading"; } return 0; } when I execute the program I always get the failure. However if I specify the full path to the file like "d:\\testdata\\debug\\testreport.txt", I am able to open the file successfully. I am using VC++ 2005 compiler and debugging with in VC IDE. How do I make VC++ to automatically take the relative path of the file. This problem only occurs when I am debugging through the VC++ IDE. Thanks and Regards
modified on Thursday, March 27, 2008 5:49 AM
You have to configure your
IDE
to used:\testdata\debug
as working directory when debugging. On Visual Studio 2005, for instance, selectProject->Properties
menu item and then (on the newly appeared window) select theConfiguration Properties->Debugging
node, selectWorking Directory
item (on theLocal Windows Debugger
) list and finally assign it thed:\testdata\debug
path. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
You have to configure your
IDE
to used:\testdata\debug
as working directory when debugging. On Visual Studio 2005, for instance, selectProject->Properties
menu item and then (on the newly appeared window) select theConfiguration Properties->Debugging
node, selectWorking Directory
item (on theLocal Windows Debugger
) list and finally assign it thed:\testdata\debug
path. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeThank you :-D :-D Setting the working directory did solved my problem. But I am not sure why the file gets opened properly if the I try to open it with methods using MFC File I/O. I never had to set the working directory when using MFC File I/O. It could probably be that MFC is setting the working directory automatically. Regards :) :)
modified on Thursday, March 27, 2008 1:42 PM
-
Thank you :-D :-D Setting the working directory did solved my problem. But I am not sure why the file gets opened properly if the I try to open it with methods using MFC File I/O. I never had to set the working directory when using MFC File I/O. It could probably be that MFC is setting the working directory automatically. Regards :) :)
modified on Thursday, March 27, 2008 1:42 PM
psychedelic_fur wrote:
Thank you
You're welcome.
psychedelic_fur wrote:
It could probably be that MFC is setting the working directory automatically.
I don't think so. Anyway now it works, why bother? Best wishes. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke