In c++ project when you have included header file, but inside project we are not able to access that header file then generally we get error for cannot open source file. For fixing this error check that included file is available in project include path or not.
cannot open source file visual studio C++
Solution-1 : Check your Visual Studio Project settings under C++, Check Include directories and make sure Your_filename.h is pointing to correct path. After adding proper include directories it will resolve cannot open source file visual studio error.
You can right click to header file and open from visual studio. If file is pointing properly it will open.
If your include file is not properly pointing or you have not added inside your included directory then you will not able to open from visual studio.
Solution-2 : Another solution, If your header file is at different folder/location then you can include file path directly in header file. Here you can use relative path or you can provide full path of header file.
Include based on your project folder structure and requirement.
#include "../FolderName/filename.h" OR #include "./FolderName/filename.h" OR #include "FullPath/filename.h"
c++ cannot open source file iostream
Solution-1: iostream file opening error normally comes when we are missing $(IncludePath) inside Properties->VC++ Directories->Include Directories. And you have by mistake removed Include path.
Solution-2: Other possible reason is that during installing Visual studio you did not selected c++ packages.
Solution-3: You can check by including stdafx.h in starting (only for Visual studio projects)
#include "stdafx.h" #include <iostream> using namespace std;
Solution-4: Sometimes these type of error comes when multiple Visual Studio versions are install in single PC. You can check Projects and Solutions –> VC++ Directories, are properly added for your project and pointing to correct version location.
For Visual Studio Code:
c++ cannot open source file iostream in visual studio code can also occurs when proper paths are not added inside visual studio code
cannot open source file “string”
Try below steps to resolve error.
1) Open VC++ Directories option inside Configuration Properties of visual studio
2) Her all directories value (e.g. Executable directories) will be available, just you need to select drop-down and click edit
3) Remove selection of Inherit from parent
4) Now just click OK and you will see no changes inside Edit box.
5) You need to repeat this thing for all directories, At the end executable directories will be set to $(ExecutablePath) and Include directories will be set to $(IncludePath), similar way to all directories
6)Now click apply and then OK
Conclusion for cannot open source file c++
We have provided solutions for multiple errors for can not open source file c++, If you try given solution hopefully you will able to resolve error. But if still you are getting error you can add your comment and code. We will try to resolve and find solution for you. Happy coding 🙂