OpenCV: fs.open fails - c++

I want to read from an .xml file, but this line
fs.open("test.xml", FileStorage::READ);
fails with cv::Exception.
\opencv_source\opencv-3.4.1\modules\core\src\persistence_c.cpp:384: error: (-49) Input file is empty in function cvOpenFileStorage\n" ...} ...} const cv::Exception &
I'm using Win7, OpenCV 3.4.1, c++, VisualStudio 2015. It's my fist time using .xml files and I'm new to opencv.
Any ideas what's wrong?

If you have downloaded the XML file using "save link as..." from chrome, then it might cause the problem. Open the file and check it once if it looks the same as in the repository.

It seems that your program is unable to find the file specified. Is the file you are trying to open in the same directory as your excecutable? Perhaps try to use an absolute path.

It requires absolute path of the file to be read. I had the same issue. Giving absolute path solved my problem.

Related

ifstream will not open

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.

sharpdx tkfxc.exe generates .tkb file when .tkfxo expected

I'm facing a problem and I couldn't find any documentation at all. Also I downloaded the github sharpdx solution but I didn't found an answer.
When I'm executing tkfxc.exe in net40 directory, the .fx effect compiles successfully but as I read on many forums, a .tkfxo file shall be generated. Instead of that I got a .tkb file.
This is the command line command I'm using:
tkfxc.exe /FoInverted.tkfxo Inverted.fx
Even if I try to change the extension to the file or just load this file as a filter, sharpDX said that is invalid file, some other CHUNK expected.

VS2010 doesnot pick up file from resources folder

I am required to parse a text file in my VS project in mfc in c++. The text file is supposed to be a part of the entire exe product. For that purpose, I placed the text file in my resources folder and set the path in my code as:
char fileName[] = "../myFile.txt";
The problem I'm facing is that VS doesn't find this file in its Resources folder. I added the file in the project file, but that just gave me a corrupt file error. However, the file access works if I provide the absolute path to the file in my code i.e. "C/abc/myFile.txt"
I need the code running on all machines, hence need some method to get VS to read this file using a relative path. Can anybody please provide some assistance? I am a newbie and have tried all that's in my knowledge.
Actually, if it's a resource file it should be copied over to the bin folder, which means your fileName should just be:
char fileName[] = "myFile.txt";
if that doesn't work then, you might need to change the properties of your myFile.txt to ensure it does get copied over with the build process.
Here you can find an answer for your question: http://www.cplusplus.com/forum/general/54255/

Can't open text file resource in Xcode 4 c++ tool?

I am working on figuring out how to use Xcode 4 to debug c++ projects.
I have basically copy pasted a working c++ executable that when compiled from the terminal ran fine.
However, i was thinking it might be nice to use Xcode for debugging. So I am trying to migrate the single .cpp file into Xcode as a command line tool.
I need to read in a file called numbers.txt (which I supply through a command line argument) which is located in my project directory, and then out put to a file (whose name I also specify as an argument.)
The problem I am running into is that the files that are supplied as command line arguments are failing to open.
ifstream in;
ofstream out;
in.open(argv[1]);
out.open(argv[2]);
I have checked to make sure that the arguments are being properly passed and are named correctly. The ifstream in is being supplied with `numbers.txt', which I want to open a text file that I already have.
However when I check to make sure the ifstream is open:
if(in.is_open() == false){
cerr << "Unable to open input file" << endl;
return 1;
}
I get the error.
I suspect this has something to do with how Xcode organizes the project.
my numbers.txt file is just sitting in the Xcode project folder, and I have only one .cpp class and one product, the executable.
anyone know what I am missing here?
The executable built by Xcode is in a different folder than the project. Passing in the name of the file without an absolute path before it will cause the executable to look for it in the wrong place, which is why it can't be found. Some of the possible solutions are to include the file as part of the build process (so it ends up in the same directory as the executable) or to pass the file to be opened by its absolute path. There are other ways to solve the problem, too, but hopefully that should be enough to get you started.
Old thread, but i have faced the same problem now, and it is easy to solve. Just copy the file in the build phase.
Here is an screenshot of the final result (note the destination, subpath and checkbox):

How to open a file in solution directory in Visual Studio c++?

I would like to open a file in the solution directory so that whenever i move the whole project file, It'll work.
I'm currently using code ! as shown below and when i tried to use code 2, it fails.
How do i do this?
Code 1:
IplImage *src=cvLoadImage("C:\\Documents\\Visual Studio 2008\\Project1\\ABC.jpg"); //A function that load image
Code 2:
`IplImage *src=cvLoadImage("$(SolutionDir)/ABC.jpg"); //A function that load image
You could use
"..\\example.txt"
that will find the file on top project directory.
Also doesn't cvLoadImage takes 2 argument! I assuming you are using OpenCV