Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Unable to open file for reading using C++ I/O [modified]

Unable to open file for reading using C++ I/O [modified]

Scheduled Pinned Locked Moved ATL / WTL / STL
c++iosvisual-studiodebugginghelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ComplexLifeForm
    wrote on last edited by
    #1

    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

    L C 2 Replies Last reply
    0
    • C ComplexLifeForm

      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

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • C ComplexLifeForm

        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

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        You have to configure your IDE to use d:\testdata\debug as working directory when debugging. On Visual Studio 2005, for instance, select Project->Properties menu item and then (on the newly appeared window) select the Configuration Properties->Debugging node, select Working Directory item (on the Local Windows Debugger) list and finally assign it the d:\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

        C 1 Reply Last reply
        0
        • C CPallini

          You have to configure your IDE to use d:\testdata\debug as working directory when debugging. On Visual Studio 2005, for instance, select Project->Properties menu item and then (on the newly appeared window) select the Configuration Properties->Debugging node, select Working Directory item (on the Local Windows Debugger) list and finally assign it the d:\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

          C Offline
          C Offline
          ComplexLifeForm
          wrote on last edited by
          #4

          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

          C 1 Reply Last reply
          0
          • C ComplexLifeForm

            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

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups