In the project i am working on, during run-time I need to open some file and read it
std::ifstream vSettings(".\\..\\..\\Data\\data.xml");
if (vSettings.good())
{
//file found ,work with it
}
I never get true for the good() call above.
Manually I checked that the file indeed exists relatively to the .exe.
the path to exe is \main\proj\bin\Debug-Win32.the path to file main\proj\Data.When running in the user computer the directory will be installed relatively to the .exe
I work in debug mode and path is as it looks from the "bin" directory point of view.
Any advises?
Go to the Debugging settings
and change the $(ProjectDir) to $(OutDir), and it should behave like you expect
Related
The file exists in the directory, and I've tried running Visual Studios in Administrator Mode. However, ifstream can not find the file I give to it.
Here is the code I am using:
std::ifstream instream;
instream.open("appdata.txt");
if (!instream)
{
std::cout << "Could not find appdata.txt!";
}
But I am always greeted with Could not find appdata.txt! when I run the program.
Here is a picture of my directory, for proof that I have it spelled correctly and it exists.
So, my question is, am I missing something so glaringly obvious that I am glazing over it each time I look? I can not figure out for the life of me why instream can not open appdata.txt.
This is a problem with current directory being set to something else than a dir where your file is (usually your home folder if executing from explorer).
Try executing the program from command line from directory where your file is.
EDIT
If you want to set the working directory to some specific location, check this: https://msdn.microsoft.com/en-us/library/aa363806.aspx
Add the file by right clicking on project name on visual studio interface.
This will keep your file in the right directory.
If you want to add in the directory by yourself, first add a file using the method I said above and the find which is the folder you should keep so that you can use that file by mentioning just the file-name. And then you can add your files in that folder.
I'm trying to use fstream to create a file, however the file i'm trying to create wont appear in the .exe directory or anywhere else in the file directory. After searching in my computer for the file, I found that was created in a different directory entirely even though i'm using a relative directory.
This is the code to create the file:
ofstream file;
file.open("something.jpg", ios::out|ios::binary|ios::beg);
Directory of created file: C:\Users\user-pc
Directory of project: D:\Users\user-pc\Documents\Visual Studio 2012\Projects\recvFile
by the way, using an absolute directory works perfectly fine. Could this be a problem with the projects working directory?
In your Visual Studio right-click on your project, click Properties, then go to Configuration Properties, then Debugging. There is a row "Working Directory". You can set the working directory there. If you need to do this programmatically, you can use SetCurrentDirectory .
If you need to create the file in the same directory as the .exe location, you can use this approach: https://stackoverflow.com/a/124901/1915854
Call GetModuleFileName() using 0 as a module handle...
If the .exe is installed in a shared directory like Program Files, then creation of the file in the same directory could require additional permissions and may be a bad idea. If the .exe is just cloned to the directory where it should create files, then there is no such problem.
Try adding "../" to the link:
file.open("../something.jpg", ios::out|ios::binary|ios::beg);
File will be created in Debug/Release folder of your project. try what Timo Rzipa suggested.
In my program, I have a button that I want to open a text file in a relative directory. I'm using QDesktopServices like this:
QDesktopServices::openUrl(QUrl::fromLocalFile("file:///stuff/block_settings.txt"));
When the button is pressed, nothing happens.
The file is in a folder named "stuff" that resides in the same location as my .exe. It is the same directory used for all my other tasks.
What am I doing wrong?
Thanks.
The file is in a folder named "stuff" that resides in the same location as my .exe. It is the same directory used for all my other tasks. What am I doing wrong?
Seems like your full path is an overcomplication. I would suggest to use this intead:
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).
Warning: On Linux, this function will try to get the path from the /proc file system. If that fails, it assumes that argv[0] contains the absolute file name of the executable. The function also assumes that the current directory has not been changed by the application.
So, you would be writing this code:
QDesktopServices::openUrl(QString("%1/stuff/block_settings.txt")
.arg(QCoreApplication::applicationDirPath()));
I fixed the issue. Changed to:
QDesktopServices::openUrl(QUrl("file:stuff\\block_settings.txt"));
Not sure how that works because I don't see that configuration on any tutorial anywhere but w/e
I'm writing a program that uses <fstream>. The file I need to include is called employee.dat and it's listed in the directory. I put in a cout statement that says:
ifstream inFile ("EMPLOYEE.DAT");
if (! inFile)
{
cout << "!!Error in opening 'EMPLOYEE.DAT"<< endl;
}
The file is in the directory and there shouldn't be any issues opening/finding it. This is my first time using the ifstream class.
If you are running from the Visual Studio environment, be aware that your executable probably isn't in the same directory as your data file. Binaries tend to be built into a Debug or Release folder by default. You have several options:
Move your data file into the correct directory.
Set the Working Directory (in your project settings, under "debugging").
Use a full or relative path for your file name.
It's also possible that you are looking in the right place, but the file is locked. Make sure it's not open in any editor or other program that might prevent other processes from opening it.
Beware that in Visual Studio, the Working Directory (set in the project properties under the Debugging tab) is the location that the exe is apparently located when debugging (and not the actual location of the exe itself)
I have written a c++ program and deployed it in say c:\my_app, and my executable's path is c:\my_app\my_app.exe. Say, my_app needs many files such as the_file.txt, which is located in c:\my_app\the_file.txt.
In my executable, I open the txt file as, xx.open("the_file.txt");
Moreover, I have associated my program with let's say .myp extension.
When I'm on Desktop, and want to open a file named example.myp, my program can not see the_file.txt. Because, it (somehow) assumes that it's currently working on Desktop.
Is there any easy way to handle this problem by changing shell command for open in HKEY_CLASSES_ROOT? The naive solution would be to change all file open operations with something like %my_app_location/the_file.txt". I don't want to do that.
Always use a full path name to open a file. In other words, don't open "foo.txt", open "c:\bar\foo.txt". To find the install directory of your EXE use GetModuleFileName(), passing NULL for the module handle.
These days you shouldn't add files to c:\my_app....
Instead use the ProgramData Folder and full paths.
Use SHGetSpecialFolderPathA with CSIDL_COMMON_APPDATA to get the ProgramData folder and the create your program directory and add your files.
You should set current directory for your app's folder with SetCurrentDirectory function. After that you can open file by name without full path