4
votes

I have been struggling for a while by the failure to open file using FileStorage: It is part of the code from: \opencv\sources\samples\cpp\tutorial_code\calib3d\camera_calibration\camera_calibration.cpp:

FileStorage fs(inputSettingsFile, FileStorage::READ); // Read the settings
if (!fs.isOpened())
{
    cout << "Could not open the configuration file: \"" << inputSettingsFile << "\"" << endl;
    return -1;
}

It always failed. Tried absolute path also, still fails. Environment: Opencv version: 2.4.8, windows 7 (32-bit) and Visual Studio 2010. (Actually, tried 2.4.2 and VS2008, same problem) Anything I am missing?

To verify permission, added regular cpp file read before FileStorage. the regular cpp file read works fine:

int main()
{
    ////Use two ways to open and read the file "in_VID5.xml"

    string inputSettingsFile = "in_VID5.xml";
    //// Method 1: cpp read file - ok
    ifstream myfile(inputSettingsFile);
    string line;
    if(myfile.is_open())
    {
        while (getline(myfile, line))
        {
            ////It does come here
            cout << line <<'\n';
        }
        myfile.close();
    }

    Sleep(1000);
    ////Method 2: opencv read file - fails
    FileStorage fs(inputSettingsFile, FileStorage::READ); // Read the settings
    if (!fs.isOpened())
   {
        ////It fails
        cout << "Could not open the configuration file: \"" << inputSettingsFile << "\"" << endl;
        return -1;
    }
    return 0;
}
3
Do you have permissions to read the file? - Marco A.
What is the extention of the file path? - David G
Is the file path ".xml", ".yml" or ".yaml"? Please select the edit button on your post and respond to us. I take it that you don't have enough reputation to comment yet. - David G
Thanks for all the replies. I did check the permission. Extension is ".xml" - Cloud Guy
To verify the permission. A few lines of regular cpp file read is added before that and it works fine. But once it comes to that FileStorage constructor and then check if it is opened, it fails - Cloud Guy

3 Answers

1
votes

I had the same problem until I realized that the constructor doesn't work with the READ flag. So what I do is: - I use the constructor with the falg APPEND. If I use WRITE the contents of the file is deleted. - Then I open with the READ flag and work with the content.

0
votes

Actually I have encountered with the same problem. After some checking I realized that the constructor of cv::FileStorage always return a bad pointer (Bad Ptr). Actually the same problem happens to other opencv library functions such as imread in the existing project.

I just solved the bug by revising the symbol file cache settings with checking the option:

Options -> Debugging -> Symbols -> Microsoft Symbol Servers

Maybe someone can give a clear explanation to the weird problem with this symptom and cure.

0
votes

Maybe the compile mode you used with the wrong library.

If your compile mode is debug, the OpenCV library must be debug version and only debug version, not include release version.

So try to check the compile profile .