I have a problem loading an openflight (*.flt) model into Openscenegraph using a simple test app:
#include <osgDB/ReadFile>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
// Setting the message level low so I can read Debug messages
osg::setNotifyLevel(osg::NotifySeverity::DEBUG_FP);
cout << "Opening flt file..." << endl;
osg::ref_ptr<osg::Node> mdl = osgDB::readNodeFile(argv[1]);
if (mdl != NULL) cout << "Opening flt file successful" << endl;
else cout << "Opening flt file failed" << endl;
return 0;
}
This should read the file pyramid.flt, which is in my executable directory by passing the file as argument to the application. However, OSG does not seem to be able to load the required Openflight plugin to indeed read the file. And thus the program returns NULL when trying to load it.
The strange thing is that the debug messages tell me that the required DLL is being used, see below:
Opening flt file...
itr='C:\dev\CgfGen\build32\3dviewer\Debug'
FindFileInPath() : trying C:\dev\CgfGen\build32\3dviewer\Debug\osgPlugins-3.4.1\osgdb_openflightd.dll ...
FindFileInPath() : USING C:\dev\CgfGen\build32\3dviewer\Debug\osgPlugins-3.4.1\osgdb_openflightd.dll
DynamicLibrary::failed loading "osgPlugins-3.4.1/osgdb_openflightd.dll"
Warning: Could not find plugin to read objects from file "pyramid.flt".
Opening flt file failed
I the pyramid.flt is in Openflight version 16.5.
I am running the debug version of my app.
OSG is loading the debug version of osgdb_openflightd.dll
Does anyone have any idea on what the problem is? and how it can be solved?
FindFileInPath() : USING C:\dev\CgfGen\build32\3dviewer\Debug\osgPlugins-3.4.1\osgdb_openflightd.dll
This just means that OSG has discovered a file matching the name of the one it's looking for, but doesn't mean that it is successfully USING it. The next line:
DynamicLibrary::failed loading "osgPlugins-3.4.1/osgdb_openflightd.dll"
Indicates that the plugin failed to load. This could be because of a problem with the plugin's own dependencies. Did you build OSG and the plugins yourself, from scratch?
You can probably use depends.exe to check and see what other DLLs your FLT plugin requires. You could also use something like SysInternals' Process Monitor to see what OSG was trying to load after
Related
I am trying to load PNG image, but it always ends in an error. I was first trying to use SDL's SDL_LoadBMP, but since that dind't work, I downloaded SDL2_image.h, did all the setup, but it still isn't working. The image is in the same directory as the cpp file. When I run my program, it runs, but prints error. Here is the function to load the image:
void loadImage()
{
image = IMG_Load("Untitled.png");
if (image == NULL)
std::cout << "error: " << SDL_GetError();
}
The error message from SDL_GetError is Failed loading libpng16-16.dll: The specified module could not be found. I tried passing the full path, but it gives an error and I don't know how to get around that problem since I am a beginner. Thanks.
Edit: After reading through the article linked in the answer to the tagged-duplicate question and making some changes(particularly installing the missing dll file and putting it in the same directory as the main cpp flie), I am getting this error message in a pop-up window: The procedure entry point inflateValidate could not be located in the dynamic link library, and the path to libpng16-16.dll is given after that.
I have the following c++ code:
int main(int argc, char *argv[]) {
std::string filename("config.txt");
if (argc == 2)
filename = argv[1];
if (argc > 2) {
std::cerr << "Too many arguments. Usage: \n"
"<program>\n"
"or\n"
"<program> <config-filename>\n" << std::endl;
return 1;
}
std::ifstream config_stream(filename);
if (!config_stream.is_open()) {
std::cerr << "Failed to open configuration file " << filename << std::endl;
return 2;
}
return 0;
}
The simple program that gets the config file as a command line argument, if this argument is not provided it searches for the config file with the default name in the current directory.
It worked fine in previous project, but now config_stream.is_open() c++ always returns false even if the file exists. I want to figure out, is this some kind of bug or I miss something.
Such confusing thing happens when you have your data file among source code files or in the root of your project.
Execution context(i.e. current directory) is usually wrong from your point of view when you run compiled binary while being in IDE.
During the build IDE creates temporary folder and places your binary there.
And current directory for the binary usually points to the build folder (where you have no data files which binary expects).
You can instruct your IDE to use another folder as a current directory for the run, for example ../.
Here is how you could configure Intellij Clion to resolve such issue.
The same thing happens with any other IDE so this issue is not specific for Intellij IDE.
I'm working on a Windows 10 Universal C++ project and I'm trying to open a binary file in read-mode using std::ifstream.
This is my code:
std::ifstream imgFile("C:\\Users\\GuiTeK\\Desktop\\picture.bmp", std::ios::binary);
if (imgFile.is_open())
{
std::cout << "OK" << std::endl;
}
else
{
int error = errno;
std::cerr << "KO: " << error << std::endl;
}
Problem is that it keeps failing with error 13, which means "The data is invalid" (C.F. System Error Codes).
However, the exact same code works fine in a Win32 Console Application C++ project.
What's wrong?
UWP apps do not have permission to access all files on the device. By default, apps can access certain file system locations such as application install directory or application data locations. For more info, please see File access permissions.
"C:\Users\GuiTeK\Desktop\picture.bmp" is a location that you app can't directly access. In UWP, we will need a FileOpenPicker to access such a file. One important rule here is that Skip the path: stick to the StorageFile.
For more info about how to handle files in UWP, please see Files, folders, and libraries and also File access sample, File picker sample on GitHub.
Like the questioner in "New to Xcode can't open files in c++?" I'm learning Xcode and OS X (I'm using Xcode 7 on a Yosemite mac).
I can get the code to work perfectly when I build and run it, but can't get the executable to work when I try to run it as a stand alone program.
I'm trying to translate some games I've written on a PC in C++ using SFML.
There has to be a way to save high scores and previous games within an app, but this has me stymied.
This is the sample code I used based on the previous question:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
string input;
fin.open("inputFile.txt");
if(fin.fail())
cout << "File failed to open." << endl;
fin >> input;
fin.close();
fout.open("outputFile.txt");
fout << input;
fout << "\n Data transferred \n";
fout.close();
}
This works perfectly when I build and run it, so I've got the proper path to the desktop folder set up. (I'm putting the data files in the same folder as the executable and specifying the path in Xcode.)
No problems when I run this within Xcode, but this is the message on the terminal console when I run the executable by itself:
"…/Desktop/datafiles/Build/Products/Debug/datafiles ; exit;
…/Desktop/datafiles/Build/Products/Debug/datafiles ; exit;
File failed to open.
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]"
Is there another flag or path that needs to be set within Xcode for this to work? Two other related questions: How do I access the terminal history to see what is going on? Finally, if I set up the project as an SFML app instead of a terminal project (command line tool app), why can't I see the files within the SFML app, even though I've set the command line flag to see hidden files, and I can see other hidden files on my hard drive? I can see the files if I open the SFML app folder in Windows, so I know they are there.
This is my first question on Stack Overflow, so apologies if this should be appended to the previous question, but this doesn't appear to be an answer to me, but is quite a different version of the original question that is not addressed in the answers.
Thanks!
I want to use c++ to open a file on Mac OS.
If I run the program under Xcode, the working directory is the same with the program, which is fine. However, if I try to run the program in terminal, the working directory is alway "Users/username". Do you know how to change the working directory to the location of the program?
Here is the sample code:
#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, const char * argv[])
{
char * dir = getcwd(NULL, 0);
cout << "Current dir: " << dir << endl;
ifstream fin("hi.txt");
if (fin.is_open()) cout << "File is Open" << endl;
else cout << "File is not open" << endl;
fin.close();
return 0;
}
Use the value $(PROJECT_DIR) in the working directory in your scheme debug settings:
You can set a custom working directory for your project in Xcode. In Xcode 4 choose Edit Scheme from the Scheme menu in the project window toolbar to open the scheme editor. Select the Run step from the left side of the project editor. Click the Options button at the top of the scheme editor. Select the Use custom working directory checkbox. Click the button on the right side of the text field to choose the working directory.
This is a really old post - updating some info for Xcode 12 (Sept 2020)
Step 1). Xcode -> Product -> Scheme -> Edit Scheme (or create a new one)
Step 2) RUN(DEBUG), Working Directory(Checkmark) and enter $(PROJECT_DIR) as a starting point.
You could use chdir(), see here: Change the current working directory in C++.
Or if you could always just issue a system call (stdlib.h): http://www.cplusplus.com/reference/clibrary/cstdlib/system/. This won't be portable, but it might be good enough for what you need.