I have a similar problem to this:
Current working directory is visual studio directory
Except that I am working with a C++ project in Visual Studio. Any suggestions?
For example if I try the solution in the following post:
GetCurrentDirectory for startup App. c++
I get this:
"C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 11.0\COMMON7\IDE"
But I want it to be the Debug folder under my project/solution folders.
Using the _fullpath command allowed me to extract the current directory. For example you can modify the example code on the linked page :
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <direct.h>
void PrintFullPath( char * partialPath )
{
char full[_MAX_PATH];
if( _fullpath( full, partialPath, _MAX_PATH ) != NULL )
printf( "Full path is: %s\n", full );
else
printf( "Invalid path\n" );
}
int main( void )
{
// Get current directory
PrintFullPath( ".\\" );
}
Related
I am making a game that can be played on mac and windows with cocos2d-x.
I first wrote the code in mac's Xcode, and it worked on mac.
When I took the project to windows and tried to build it in Visual Studio 2017, an error occurred.
Error C1083 Cannot open include file: 'cxxabi.h': No such file or directory Narazumono c:\users\masanori\desktop\narazumono3.17\classes_win\nrzcoding.cpp 10
I use cxxabi.h to get the class name of the object.
#include "NRZCoding.h"
#include <cxxabi.h>
#include <cstdio>
#include <algorithm>
#include "NRZUtils.h"
using namespace std;
USING_NS_CC;
namespace NRZCoding {
...
const string& EncodableObject::getClassName()
{
if (_className.size() > 0) {
return _className;
}
const type_info& id = typeid(*this);
int stat;
char *name = abi::__cxa_demangle(id.name(),0,0,&stat);
CCASSERT(name != NULL && stat == 0, "failed to demangle");
_className = string(name);
free(name);
return _className;
}
...
What do I need to do?
I am using cocos2d-x 3.17.1 and Visual Studio 2017.
Thank you.
Good evening everyone, please I'm writing a Library management application in c++ using virtual studio 2012. I had already writing some codes using Dev c++ it worked but when I switched to visual studio it gives error. It involves creating folders and checking if the folders were actually created. That is using dir and mkdir.
Windows and Linux (POSIX) don't support the same API for most file system functions. You can use Microsoft's platform-specific APIs like CreateDirectory() or use the POSIX-like versions like _mkdir().
If you have a more recent C++ compiler / standard library, you can use the experimental filesystem library that is slated to become part of standard C++, perhaps as early as C++17. If not, you can use Boost.Filesystem from which the pre-standard experimental library was drawn.
Here's a complete, minimal example using Boost.Filesystem, which will work on both Windows and Linux without modification:
#include <iostream>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
int main()
{
if( !fs::exists( "my_dir" ) )
{
if( fs::create_directory( "my_dir" ) )
{
std::cout << "Directory created!\n";
}
}
}
See it run: Coliru.
Here's the same code but with std::experimental::filesystem: Coliru.
You would need the appropriate include and linker paths setup in your build system for either of these to work locally. The biggest "gotcha" using the filesystem is that it throws exceptions for a lot of errors by default. You can either setup try/catch blocks at the appropriate places or pass in an error code param to make it return the status there instead.
#include <stdio.h>
#include <windows.h>
int main() {
if (!CreateDirectoryA("C:\\ABC123", NULL))
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
printf("Already Exists");
}else if (GetLastError()== ERROR_PATH_NOT_FOUND)
{
printf("Path not found");
}
}else{
printf("Created..");
}
}
simple function will do.
Thanks alot guys but I found this that solved my problem
#include <iostream>
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
void main( void )
{
if( _mkdir( "\\testtmp" ) == 0 )
{
printf( "Directory '\\testtmp' was successfully created\n" );
system( "dir \\testtmp" );
if( _rmdir( "\\testtmp" ) == 0 )
printf( "Directory '\\testtmp' was successfully removed\n" );
else
printf( "Problem removing directory '\\testtmp'\n" );
}
else
printf( "Problem creating directory '\\testtmp'\n" );
int a;
cin >> a;
}
the cin >> a; is just to keep the output screen so I can see the result
This is the code I have for checking if a file exists in my visual studio 2010 c++ project:
bool GLSLProgram::fileExists( const string & fileName )
{
struct stat info;
int ret = -1;
ret = stat(fileName.c_str(), &info);
return 0 == ret;
}
I am not sure why it returns false for "shaders/color.vert" when that file really exists, and shaders is a folder in my project main folder.
Can you see something wrong?
THanks
Ok, so to illustrate the quirks of running from the IDE here's a little test I did. Hopefully this should help you figure out how relative paths work in VS.
So my folder hierarchy looks like this:
/_Sandbox
_Sandbox.sln
/Debug
_Sandbox.exe
/shaders
color.vert
/_Sandbox
_Sandbox.proj
main.cpp
The code looks as follows:
#include <iostream>
#include <string>
#include <sys/stat.h>
int main(int argc, char* argv[])
{
struct stat info;
std::string path = "shaders/color.vert"; // To not I get the same behavior with "shaders\\color.vert"
int ret = stat(path.c_str(), &info);
ret == 0 ? std::cout << "File found." << std::endl : std::cout << "File doesn't exist." << std::endl;
std::cin.get();
return 0;
}
So if I run this in the IDE, I get "File doesn't exist.", if I run this outside the IDE, I get "File Found". In order for the program to find the shader file from inside VS I have to put the shader folder like so:
/_Sandbox
_Sandbox.sln
/Debug
_Sandbox.exe
/_Sandbox
/shaders
color.vert
_Sandbox.proj
main.cpp
You can however get the code to find the folder from inside and outside the IDE. What you have to do is go to your project's settings. In "Debugging" and change "Working directory" to $(SolutionDir)$(Configuration)\
Hopefully this clears things up for you.
I find that the API,
_tfsopen()
is not working when I just give the relative file path (JUST FILE NAME) in
fopen_s(&fp,"FileName.txt",w+);
But if I give the absolute path of the txt file then it's working fine. So the problem is in getting the current working directory (CWD). How do I get the CWD and make this API work?
This API definition is present in:
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\crt\src\fopen.c
This is what fopen.c has:
errno_t __cdecl _tfopen_s (
FILE ** pfile,
const _TSCHAR *file,
const _TSCHAR *mode
)
{
_VALIDATE_RETURN_ERRCODE((pfile != NULL), EINVAL);
*pfile = _tfsopen(file, mode, _SH_SECURE); // ERROR IS OCCURING AT THIS LINE. IT's NOT ABLE TO OPEN THE FILE.
if(*pfile != NULL)
return 0;
return errno;
}
This code let you get current working directory:
#include <direct.h>
char cwd[MAX_PATH_SIZE];
_getcwd(cwd, MAX_PATH_SIZE);
I am writing a program that will open an image file, but strange thing happened. This is the output from cmd:
C:\Users\Karl\Pictures>testcvconsole mypic.jpg
argv[0]==testcvconsole
argv[1]==mypic.jpg
fopen is null
strerror(errno)==No such file or directory
Are there something I should consider when fopen simply failed to open my file when the file is right there along side with the executable file in the same directory?
This is on Windows 7, Visual Studios Express 2010. C++.
EDIT: code below
#include "stdafx.h"
#include <string.h>
#include <errno.h>
int goMain(int argc, char** argv);
int _tmain(int argc, _TCHAR* argv[])
{
goMain(argc, (char**)argv);
return 0;
}
int goMain( int argc, char** argv ){
if (argv[1] != NULL){
printf("argv[0]==%S\nargv[1]==%S\n", argv[0], argv[1]);
if (fopen(argv[1], "r") == NULL){
printf("fopen is null\n");
printf(strerror(errno));
}
}
return 0;
}
EDIT2:
I have tried
char *workingDir =_getcwd(NULL, 0);
printf("workingDir == %S", workingDir);
as TomK has suggested and it returned:
workingDir ==
Nothing at all. Hmm...
EDIT3:
I am getting something. I tried
argv[1] = "C:/Users/Karl/Pictures/mypic.jpg";
And fopen can open it. This statement above is inserted right before the fopen.
Make absolutely sure they are in the same directory. I'm saying this because you're using Visual Studio, for which the "same" directory isn't always so clear, because it depends on how you execute the executable through the IDE.
C:\Users\Karl\Pictures>testcvconsole mypic.jpg
Are you sure mypic.jpg is located in C:\Users\Karl\Pictures ?
Can u check whether the working directory is correct?
#include <direct.h>
char *workingDir =_getcwd(NULL, 0);
Can you run your application with admin privileges?
Usually the .exe is created in sub-directory either Debug or Release - try giving the absolute path to the image ...
I've had this problem, and it turned out that Visual Studio's runtime wasn't setting the current directory. I never figured out the problem: instead I simply used an absolute path. Without the absolute path, your program is looking in C:\. You can also try using ".\\mypic.jpg" or GetCurrentDirectory().