i just found a little piece of code that let me create a directory with windows API without using system(). The only problem is that i can't create directory in subdirectory.
For example
#include<windows.h>
int main(){
CreateDirectory ("C:\\random", NULL);
return 0;
}
Create a folder named random in C.
But if i do
#include<windows.h>
int main(){
CreateDirectory ("C:\\Users\morons", NULL);
return 0;
}
It creates in C che folder named Usersmorons and not the folder morons under Users.
Any suggest?
You will need admin access to create or delete a folder in C:\Users. Make sure that you are running the .exe as admin, to ensure you have these privileges. If you do not, then CreateDirectory will fail.
To get the error that is returned, use GetLastError. For a reference on the errors that may return, please take a look at the "Return value" section at
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363855%28v=vs.85%29.aspx
Also, the code you are looking for is
CreateDirectory ("C:\\Users\\morons", NULL);
As there needs to be a "\\" before "morons"
You need another backslash in there:
CreateDirectory ("C:\\Users\\morons", NULL);
Making a windows application cross-platform, was using CreateDirectory() c++17 standard now has std::filesystem::create_directories
#include<iostream>
#include <filesystem>
int main()
{
std::filesystem::create_directories("C:\\newfolder\\morons");
}
Needs changes in makefile to -std=c++17 and updating to a GCC compiler that supports c++17.
In visual studio follow steps here to enable c++17
Related
I have a program defined in a certain directory A, and a shortcut to it in another directory B, and I have B added to PATH. In a different directory C I am running cmd.exe and am executing this program with the command progname.lnk arg1 arg2. My understanding is that the "current working directory" in this situation is C, but when I use _getcwd (see below) to obtain the current working directory I get directory A, the place where the program is kept.
#include <iostream>
#include <direct.h>
#define GCWD _getcwd
int main() {
char cwd[256];
GCWD(cwd, 256);
std::cout << cwd << std::endl;
}
I compiled and ran this program with the command mwe.lnk. This program is stored in A, has a shortcut to it in B, and I ran it from C, just like my actual program.
This code is informed by computinglife's answer to a related question, which is the same technique used here.
Is my understanding of what a "current working directory" is correct? It seems to agree with the first paragraph of computinglife's answer, so what am I doing wrong?
When creating shortcuts Windows configures them to set the working directory to the location where the program is stored. Emptying this field in the shortcut properties solved the problem.
I'm trying to write a simple SGX project for a start. So I have this main host application routine that I've pretty much copied from Lars Richter's blog:
#define ENCLAVE_FILE _T("Enclave.signed.dll")
#include <tchar.h>
#include <cstdio>
#include "sgx_urts.h"
#include "Enclave_u.h"
int main()
{
sgx_enclave_id_t eid;
sgx_status_t ret = SGX_SUCCESS;
sgx_launch_token_t token = { 0 };
int updated = 0;
ret = sgx_create_enclave(ENCLAVE_FILE, SGX_DEBUG_FLAG, &token, &updated, &eid, NULL);
if (ret != SGX_SUCCESS) {
printf("\nApp: error %#x, failed to create enclave.\n", ret);
}
scanf("\n");
return 0;
}
It compiles fine (I'm using the Intel C++ 17.0 compiler with Visual Studio 2015) but it doesn't load the enclave. I get the following error message:
[sgx_create_enclavew ..\urts\win\urts.cpp:195] Couldn't open file with CreateFile()
App: error 0x200f, failed to create enclave.
Go to app_test_save project setting. Under Debugging, change working directory to $(SolutionDir)Debug. This answer assumes that both projects app_test_save and enclave_test_save belong to the same solution.
As Neil pointed out, sgx_create_enclave couldn't find the dll when the program was being run from within Visual Studio's debugger. It worked fine when I directly ran the executable in the "Debug" folder.
So a simple trick to make it work in both settings is to do the following:
#define ENCLAVE_FILE _T("../Debug/Enclave.signed.dll")
According to this : https://software.intel.com/en-us/forums/intel-software-guard-extensions-intel-sgx/topic/623738
If you are using the Local SGX Debugger, Please make sure change the "current working directory" pointing to $(OutDir) instead of $(ProjectDir).
Configuration Properties --> Debugging --> Working Directory --> $(OutDir).
Error is basically means it could not locate your .dll file.
Do dir /a/s to find Enclave.signed.dll then change the name appropriately.
When you create enclave it will generate signed.dll file. If your enclave name is Enclave12 then the DLL name is Enclave12.signed.dll. You fix this then you should be good to go.
Does anyone know why the file isn't opening? I also tried just putting "infile.txt" and placing it in the folder of the program and also the debug folder but the ways I used to check for open error both triggered meaning that it could not open. I know I can hard code the location but I don't want to.
I heard you should do stringobj.c_str() but I don't know if that's accurate?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
string fileloc = "infile.txt";
infile.open(fileloc);
if (!infile)
{
cout << "open fail 1" << endl;
}
bool fail = infile.fail();
if (fail)
{
cout << "open fail 2";
}
return 0;
}
Note that the directory structure (at least for VS2013) is
<base>
- Solution Directory
- Debug
- Release
- Project Directory
- Debug
- Release
The program by default runs in the project directory (even though it is built to the solution/debug directory).
If you accepted the default naming convention when starting your project, you should be putting your file in the "Projects\ConsoleApplication1\ConsoleApplication1" directory, not "Projects\ConsoleApplication1"
Check your working directory in Project Settings -> Debugging. Make your file available there.
First, the documentation for the signature of
std::ifstream::open( const char * filename, ios_base::openmode mode=ios_base::in)
does indicate it requires a const char *, exactly what std::string::c_str() provides. However, there is an overload for open which accepts a const str &, which means it works the same way for both on most implementations.
Otherwise, what you're grappling with is known as the current working directory (or cwd). Apparently you're not sure where THAT directory is. It may be different while you run the debugger on Visual Studio than it is when you run your program from the command line, and it may be different in various IDE's.
I'm not sure why you want to ensure your program only opens a file by name in the current directory, and not give the full path, but...
You may want to inquire what the current working directory is, so you can solve the mystery wherever you try this. In my Visual Studio 2015, the directory ends up being the directory ABOVE debug, but that depends entirely on how your project is configured, and we can't see that out here.
So, try:
std::string cwd = getcwd( NULL, 0 );
This requires a header <direct.h> on Windows in Visual Studio, but it will give you the directory you're trying to figure out.
with
string fileloc = "infile.txt";
if you put infile.txt in the same folder of the cpp file, it should be fine.
btw I delete your first line
#include "stdafx.h"
I use cygwin console, may have minor diff
For my issue - i was stuck at loading image by opencv - i was wrong to place directory with jpg in the root of the C++ project
WRONG:
CORRECT:
I'm trying a very basic C++ program using Code::Blocks. I'm on Ubuntu 12.04 and installed pqxx from the software manager. Here's the code.
#include <pqxx/pqxx>
#include <iostream>
using namespace std;
int main()
{
pqxx::connection MyConn ("dbname=dbESM user=postgres");
cout << "Hello world!" << endl;
return 0;
}
But I get the following error on hitting F9 to compile and run:
/usr/include/pqxx/connection.hxx|87|undefined reference to
`pqxx::connectionpolicy::connectionpolicy(std::basic_string, std::allocator > const&)'
The above message is from the file connection.hxx and the line highlighted is this:
explicit connect_direct(const PGSTD::string &opts) : connectionpolicy(opts) {}
The connection.hxx file is not mine - I think it's part of pqxx.
I'm pretty new to this platform so I'm avoiding the terminal to compile code. Any help would be greatly appreciated.
You need to add the reference to the libpqxx library to the project.
Inside Code::blocks, when the project is open, locate Project in the menus, then follow Build options, then open the tab called Linker settings, then hit Add, then enter pqxx.
If you were using the libpq C library instead, the procedure would be identical except the name would be pq.
You need to link against the according library, just #including the header files isn't enough. If available, you could use pkg-config to determine the according libraries. Further, what IDE are you using? Without that, the "on hitting F9" reference is useless. Also, compiling this on the commandline might even be easier, since it is clearer what exactly is happening.
For example: I'm on MS DOS, I have a source code in the folder C:\Documents and Settings\Programs. Can i make my source code use a program (for example gnuplot) that is in a random folder?
http://www.codeproject.com/KB/system/newbiespawn.aspx
ShellExecute will look into the PATH environment variable, so you don't need to specify the full PATH. Now, if it's really a random location and it's not even in the PATH environment variable, then I guess you are out of luck.
If they aren't even in the PATH, then you have to search for it in the candidates folder. Here's sample code on how to traverse a file system path in C++.
And an example using Boost:
directoryList.h
#ifndef DIRECTORYLIST_H_INCLUDED
#define DIRECTORYLIST_H_INCLUDED
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include <iostream>
#include <list>
#include <string>
class directoryList {
public:
directoryList();
~directoryList();
std::list<std::string> getListing(std::string path);
};
#endif // DIRECTORYLIST_H_INCLUDED
directoryList.cpp
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/convenience.hpp"
#include "boost/filesystem/path.hpp"
#include "boost/progress.hpp"
#include "directoryList.h"
using namespace std;
namespace fs = boost::filesystem;
directoryList::directoryList() {}
directoryList::~directoryList() {}
list<string> directoryList::getListing(string base_dir) {
list<string> rv;
fs::path p(base_dir);
for (fs::recursive_directory_iterator it(p);
it != fs::recursive_directory_iterator(); ++it) {
string complete_filename = it->path().string();
rv.insert(rv.begin(),complete_filename);
}
return rv;
}
Usage sample:
directoryList *dl = new directoryList();
filenames = dl->getListing("C:\\Program Files");
//search for the file here, or modify the getListing to supply a filter
Here are some options:
Search in the system PATH for the executable you want to run
Allow the user to specify the location on the command-line
Store the location in a configuration file, and allow the user to specify it during install (if you have an install process) or by editing the file by hand
Ideally you'd do all 3
Also there are some core functions _exec/exec and its modifications. Similar functions are available for Linux.
The location of source code has nothing to do with the way programs are located by system() call (I assume you use that call). The only relevant consideration is the location of the compiled executable.
Please take a look at PATH environment variable in Windows - this is how programs are found. It's a semicolon-separated list of directories where Windows looks for executables and BAT files and DLLs. To that list, current directory and (I think) the place where your EXE is are prepended.
The PATH is set in Windows XP from System control panel widget Advanced tab Environment button. For Vista, things are more complicated - you need to do it as administrator.
As Vinko said, the PATH environment variable determines where Windows will look for program files.
It's usually best to avoid hard-coding the path of an executable into your compiled program. Even if gnuplot is in a particular folder on your machine then it might well not be in the same folder on someone else's computer. This would cause your call to the other program to fail. You could store it in the registry and let the user configure the program location, or provide an installer that searched for it.