I'm using boost to create a directory to place some temp files in.
int main( int argc, char* argv[] )
{
std::cout << "Current Dir: " << argv[0] << std::endl;
boost::filesystem::create_directories( "TempFolder" );
return 0;
}
Now if double click the exe, the folder "TempFolder" is created in the same directory as the exe, which I expect. However if I now drag a file onto the exe the folder is created in "C:\Documents and Settings\0xC0DEFACE" which i certainly was not expecting.
Seeing my app hasnt changed, and the dir being printed out hasnt changed, and my app is currently ignoring passed strings, why is the folder now being created in a new directory?
im running windows XP, with VS9 and am using boost 1.39.
I think it's because of the way you 'execute' your binary.
In the first case you double click it and it will run in 'current' directory.
In the second case you drop file on it which causes different action by Windows to execute your binary. In the second case the binary runs in your 'home' directory I believe.
It's the difference between how Windows executes your application.
I've had similar issues when dropping files on my executable.
Related
I have inherited a project to work on and the initial build was developed on linux. I dont know if this matters or not but thought I would share it.
In order to debug the project on a windows machine I first use the CMakeGUI on win7 to create a Visual Studio Solutions file to open the project using Visual Studio 2013 and then set the startup project and build the project I am interested in. Up till now everything is okay. Now comes the confusing part.
On load the program is suppose to read a file lets call it in.dat and is declared in const char * inputFileName this variable is then passed through a class which attempts to open then file to obtain data.
fstream fs;
fs.open(inputFileName.c_str(), fstream::in);
if(!fs.is_open())
{
std::cout << "Cannot open input file!" << std::endl;
exit(0);
}
This where I am stumped...the file when placed in the debug folder for some reason cannot be opened i.e fs.is_open() returns false when I try debugging the application BUT if I cd directly into the debug folder of the project, outside of VS, and run the executable it runs as expected i.e fs.is_open() now returns true.
Your debugger's working directory defaults to your project's root directory and the binary is in the \debug subdirectory, resulting, in effect, to the path to the input file being wrong.
Set the debugger's working directory to \debug.
Here is more info on that:
https://msdn.microsoft.com/en-us/library/kcw4dzyf(v=vs.120).aspx
I have learned the basics of C++ but I have never used visual studio.
I would like to know why I get popup window that says "Unable to start program" and then lists a file path C:\folder\folder\folder\../../lib/Win32DB/ProjectNameDB.lib. (The message doesn't give me any more info, like 'the system cannot find the file specified' or anything like that.)
ProjectNameDB.lib exists, but not at that particular location. The project builds successfully, and the same path as above appears in the output after TargetPath =.
I have tried setting the project as startup, deleting .suo files and vcproj.user files, starting without debugging, and putting the location of ProjectName.lib in the Output, Library, Include, Reference Directories.
You can not start one *.lib but one *.exe. So build one EXE program you should use the below steps with Visual Statio 2013:
start vs2013;
choose File -> New -> Project;
choose Win32 Console Application, and write your project name, click OK;
click Next, click Finish;
Now, you can write "Hello World" in 'x.cpp'(here 'x' is your project name); the following code:
int main(int argc, _TCHAR* argv[])
{
printf("Hello Wrold!\n");
return 0;
}
save, build and start run it, it will print 'Hello World' in console.
exe file must have main function, but lib file is not necessary.
So... the problem was that the project was configured to run as a static library and not as an executable.
Properties -> Configuration Properties -> General
Im getting two different paths when i run the same build within Qt Creator and when I double click on it from the Finder on a Mac.
Here is my code:
QDir dir = QDir::currentPath();
dir.cdUp();
dir.cdUp();
dir.cdUp();
QString rootPath = dir.absolutePath();
When I run it (debug) mode in Qt Creator my path is:
/Users/myuser/Projects/AppName/build/mac
When I double click on the file that is located on
/Users/myyser/Projects/AppName/build/mac from finder it returns
/ only.
Why would I get two different paths?
Version: Qt5.2.1
Update
Seems like its a bug from reading the following URLhttp://qt-project.org/forums/viewthread/34019
Why would I get two different paths?
As they write in the thread you linked, QDir::currentPath() does not necessarily returns the application directory. It will return the path from wherever the application is run, which will be different than the application directory when running the application from the command line, or even from "start menu" alike places and so on.
If you wish to deal with the application directory to navigate from there, you would need to use the following method instead:
QString QCoreApplication::applicationDirPath() [static]
Returns the directory that contains the application executable.
For example, if you have installed Qt in the C:\Qt directory, and you run the regexp example, this function will return "C:/Qt/examples/tools/regexp".
On Mac OS X this will point to the directory actually containing the executable, which may be inside of an application bundle (if the application is bundled).
The last sentence even clarifies the Mac OS X case.
The current directory can be anything, it solely depends on how your process is launched. What you've shown so far is that Qt Creator and Finder start the process with different current directory, that's all.
The only use for currentPath without setting it first, that I can think of, is in command line / console applications. Why do you think you need to use it? To what end?
This is pretty bare-bones, meant to just get the program going so I can debug the more complex parts:
//open file
cout << "Input File Name: ";
string fileName;
cin >> fileName;
ifstream file;
file.open(fileName, ios_base::in);
if (!(file.good())){
cout << "File Open Error\n";
return 0;
}
The program compiles fine. If I execute the debug executable from \Projects\[this project]\Debug\[program].exe by just double-clicking or browsing there via cmd, it will open the file (which is stored in that same directory) and the rest of the program hums along nicely (until it gets to the buggy parts I actually want to debug, anyways).
However, if I try to 'Start Debugging' from within VS2013, the above fails; it prints the error and immediately closes. The cmd window that the program is executing in when in debugging mode of course shows the directory in the title area, and it is definitely the same directory, but I guess it's looking for the file somewhere else. I tried copying it to the volume root as well, no joy there. I am certain this worked just fine in earlier versions of VS, but maybe I'm just brain-farting here. Any ideas?
In the project properties in Visual Studio, you can set the current directory in which to start the debugged program. By default, this is set to the location of the .vcxproj file, i.e. it's not the location of the executable.
At the same time, relative paths passed to std file stream constructors are interpreted relative to the program's current directory, which is why it fails for you when debugging but not when running directly. If you instead launched the program using these cmd commands:
>cd some\random\dir
>C:\path\to\your\Projects\[this project]\Debug\[program].exe
It would fail in exactly the same way.
To modify the startup directory used by Visual Studio when debugging, go to Project > Properties > Configuration Properties > Debugging > Working Directory. Note that the setting is configuration-specific (i.e. you can have a different startup dir for each configuration). If you want to set it to the directory containing the executable, you can use the macro $(OutDir).
Perhaps preferably, you might want to move the data file into the project source directory, as it's not a build artifact.
Ok, so this is the first time I've coded C++ in Xcode (I'm used to ObjC)and I've now started a programming course at my college.
I'm trying to open a file (either hard coded or from user input in the console) and no matter what I try, it says the file won't open (through error checking)
I'm assuming it's because the test.txt file I have isn't in the assumed root directory, so if that's the case, what is the root directory?
Here's my code so far:
//include files
#include <iostream>
#include <stdio.h>
#include <fstream>
using namespace std;
//Global Variables
short inputPicture[512][512];
short outputPicture[512][512];
//Function Prototypes
void getInput(char* in, char* out);
void initializeArray(ifstream* input);
//Main
int main(){
//local variables
char inputFile[32];
char outputFile[32];
ifstream input;
ofstream output;
getInput(inputFile, outputFile);
cout << inputFile << endl;//test what was sent back from the function
input.open(inputFile, ifstream::in);
if (!input.is_open()){//check to see if the file exists
cout << "File not found!\n";
return 1;//if not found, end program
}
initializeArray(&input);
return 0;
}//end Main
//Gets initial input from user
void getInput(char* in, char* out){
cout << "Please designate input file: ";
cin >> in;
cout << "\nPlease designate an output file: ";
cin >> out;
}//end getInput
//Sets the global array to the information on the input file
void initializeArray(ifstream* input){
}//end initializeArray
Please let me know if there's something else wrong I'm doing, as I'm sure that's always a great possibility :)
The default directory should be relative the application's working directory, which is usually the same place the application is located (debuggers can mess with that, sometimes).
For simple testing, just specify an absolute path in the command line (or code).
To get the current directory (to see), the getcwd() C function (also usable in C++) will help. Something like:
char * dir = getcwd(NULL, 0); // Platform-dependent, see reference link below
printf("Current dir: %s", dir);
That should display it in the console. The getcwd function has a few variations depending on what you run on, I've not tested on Mac, but info here:
http://linux.die.net/man/3/getcwd
In the Xcode (v7.1.1 at the time of writing) sidebar, there's an automatically generated folder called "Products". Inside you'll find the executable of your project (assuming you've built your project at least once). Right-click it & choose "Show in Finder". A folder will open in Finder. That's the working directory of your program, & you'll notice it's not actually inside your project's folder.
You can have Xcode use a different directory instead. In the top toolbar, on the left side where it shows your project name next to the active build architecture, click on the Project name > Edit Schemeā¦
Then look for an option called "Working Directory" in the sheet that appears. Tick the checkbox & then choose a custom directory. Note: Make sure the "Run" option is the selected one in that sheet's sidebar.
The "root" directory that your executable is looking for the file in is not the actual / root directory of the file-system, but is the directory that the executable is executing in ... if you are using Xcode, this may be buried inside one of the build directories automatically created by Xcode for your project rather than a user home folder or home folder sub-directory like /Users/XXXXXX/Documents.
The "default directory" is the directory from which the executable was executed. Usually this is in the same folder as the executable, although if you do stuff like dragging and dropping files on an exe, it can change the startup path.
The path can also change if you're running the program from inside your IDE. The IDE starts the executable, so there's no telling where it's doing it from. You'll have to find where it stores executables and put the file in there, or use an absolute path.
In my case,
- getcwd(NULL, 0) returned "/".
- and couldn't use abusolute path.(It changes on each terminal deployed.)
So I got the path by ObjC code and throw it to c++ function.
1.Put files in top directory of xcode project. And check they are included in "Targets"->"Build Phases"->"Copy Bundle Resources".
2.Get the path by ObjC code.
NSBundle* bundle = [NSBundle mainBundle];
NSString* resourceDirectoryPath = [bundle bundlePath];
NSString* path = [resourceDirectoryPath stringByAppendingString: #"/"];
3.And throw it to c++ function.
[self cppFunc:[path UTF8String]];
4.Then you can make an ablsolute file path in c++.
std::string file = path(arg) + "filename";
It worked for me.