Inputting values from .mat file to array in C++ - c++

I am trying to copy a 1100x1100 matrix from a .mat file to an array variable of type float in C++. I read online and found that the matio library is a good option. I installed their library using "make" on Ubuntu 12.04 (I followed the method given on their webpage).
However, I am unable to write code using it mainly because I am new to C++. I am using g++ to compile the file. I get errors such as "unknown reference to Mat_Open" and so on.
I did find this bit of code on the webpage:
#include <stdlib.h>
#include <stdio.h>
#include "matio.h"
int main(int argc,char **argv)
{
mat_t *matfp;
matvar_t *matvar;
matfp = Mat_Open(argv[1],MAT_ACC_RDONLY); //here argv[1] is "a.mat"?
if ( NULL == matfp )
{
fprintf(stderr,"Error opening MAT file %s0,argv[1]);
return EXIT_FAILURE;
}
matvar = Mat_VarReadInfo(matfp,"x"); // x is the variable we are trying to access?
if ( NULL == matvar )
{
fprintf(stderr,"Variable ’x’ not found, or error reading MAT file\n");
}
I have a couple of questions:
here, argv[1] corresponds to the .mat file I am trying to open right?
x in this code is the variable present in the .mat file I am trying to copy?
When I ran this code, I received errors stating - Unknown reference to Mat_Open and so on. Another couple of the same type of errors also were there.
I compiled this using : g++ abc.cpp -o test. (Followed by ./test. But I never got around to that due to the errors obtained during compilation).
How can I make it work? Is there any mistake with the code I used? Or with the compile statement I am using-maybe there are some linkers I need to use for compilation.
Thank you. Please remember that I am new to C++. Any advice would be helpful.

1) argv[1] - is a first parameter you put after your program call. If you want to "feel it", use debugger or code like this:
#include <iostream>
for (unsigned i = 0; i < argc; ++i)
{
std::cout << argv[i] << std::endl;
}
2) Yes, looking at http://libmatio.sourcearchive.com/documentation/1.3.4/group__MAT_g4c8205ff25c5b688a40775fbb1840b7e.html I can say, that you will read variable with name "x".
3) "undefined reference" means you need to build with matio libraries. Add something like "-lLibraryName" to your compile string. And it will have to be built.
To avoid many problems, try to install Code::Blocks, it's cross-platform and quite easy to start using C++ if you never did it before. It also supports debuggers, so you will avoid many problems quite easy.

Related

Using sqlite3_load_extension with a windows DLL

Can anyone tell me where I'm going wrong here?
I'm using C++ bulder 10.2 (clang compiler). From IDE menu I've selected File|New|Dynamic Link Library and chosen compile as C with no dependencies. I've then added sqliteFcts.c and made the sqliteFcts.dll library. It all compiles fine.
Contents of sqliteFcts.c.
#include <sqlite3ext.h>
SQLITE_EXTENSION_INIT1
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_sqliteFcts_init(
sqlite3 *db,
char **pzErrMsg,
const sqlite3_api_routines *pApi
){
int rc = SQLITE_OK;
SQLITE_EXTENSION_INIT2(pApi);
return rc;
}
// My code is above. Code below was created by the IDE
#pragma argsused
int _libmain(unsigned long reason)
{
return 1;
}
From my own app I try to use the dll with the following code
int rc;
sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION,1,&rc);
rc=sqlite3_load_extension(db,"C:/temp/sqliteFcts.dll",0,&buf);
I get the error message "The specified procedure could not be found". From what I've read windows expects _libmain to be the Entry Point but sqlite expects sqlite3_sqliteFcts_init to b e the Entry Point and I've no idea how to resolve that or if that is even the problem.
I finally got it working but with a loose end.
Firstly in this line
rc=sqlite3_load_extension(db,"C:/temp/sqliteFcts.dll",0,&buf);
I supplied a null for the 3rd parameter (the entry point) thinking sqlite would resolve this however sqlite would've supplied sqlite3_sqlitefcts_init (note lower case f) as the entry point so, inevitably, the function wouldn't be found. The line should read
rc=sqlite3_load_extension(db,"C:/temp/sqliteFcts.dll","sqlite3_sqliteFcts_init",&buf);
That said, it still didn't work. After advice I received on the sqlite forum I downloaded Dependency Walker and found the function listed as _sqlite3_sqliteFcts_init (note opening underscore) although I've no idea why. Anyway, I changed the above line to
rc=sqlite3_load_extension(db,"C:/temp/sqliteFcts.dll","_sqlite3_sqliteFcts_init",&buf);
and it worked. That was for a 32 bit implementation. I later discovered that when it was compiled as a 64 bit app the opening underscore wasn't required. If anyone can throw any light on this quirk I'd be grateful.

gnuplot.exe path not found for writing in c++

I'm new to gnuplot and was trying to just make a plot of random numbers to visualize the distribution of the psuedo-random rand() function in c++. However, I came across a problem that appears common: the path was not found for gnuplot.exe.
Possible relevant information before I go on:
Running Windows 10 64-bit. Compiler: CLion. gnuplot.exe path: C:\Program Files (x86)\gnuplot\bin I have the latest version of gnuplot form sourceforge (as of 10/21/19: 5.2.7 I believe).
I have already checked these questions:
gnuplot-cpp cannot feed command to pipe
Gnuplot & C++: Can't find gnuplot neither in PATH nor in "
I've looked through many other questions but they weren't as relevant to the problem I have. I tried implementing the answers there and did not make any progress. I have the same gnuplot_i.hpp file and changed the problem line to:
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
std::string Gnuplot::m_sGNUPlotFileName = "gnuplot.exe";
std::string Gnuplot::m_sGNUPlotPath = "C:/Program_Files(x86)/gnuplot/bin/";
I also have the correct PATH set for gnuplot as a environmental variable and can execute gnuplot from cmd.
The errors I've gotten were:
Cannot find gnuplot neither in PATH nor in "C:/Program_Files(x86)/gnuplot/bin"
and, when I removed the "_" from the path above and replaced it with a space:
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
This last one is the only one that appears now, even though I do not have the space anymore, as it's been mentioned that having spaces in the path is a known bug with gnuplot. I am aware that Program_Files(x86) is not a real path, I was just trying to get rid of the second error message.
My main.cpp, if it even matters:
#include <iostream>
#include "gnuplot_i.h"
#include <cstdlib>
#include <vector>
using namespace std;
int main(int argc, char* argv[]) {
try {
Gnuplot rand_plot("lines");
}
catch (GnuplotException ge) {
cout << ge.what() << endl;
}
return 0;
}
Any help on solving these errors would be greatly appreciated.
Botje in the comments solved the issue. Changing lines 1714-1715 in gnuplot_i.h to std::string tmp = std::string("\"") + Gnuplot::m_sGNUPlotPath + "/" + Gnuplot::m_sGNUPlotFileName + std::string("\""); fixed the problem.

Get parameter from exe in C++

As we know the Query String in web. It's key/value go with the website URL ex: abc.com?myName=stack
For example in PHP, if we want get value of the myName, just do this $_GET['myName']
So, in C++, how can I get it?
in C# I pass an parametter to an *.exe file ( this exe file is C++ code ).
In C++ code, how to get this parametter value .
Build a console application with just the following code:
#include <iostream>
int main(int argc, char** argv)
{
for(int i = 1; i != argc; ++i )
{
std::cout << argv[i] << std::endl;
}
}
Assuming the name of the .exe is mytest.exe, execute it with some arguments, such as:
mytest.exe Hello there.
You should get the following output:
Hello
there.
Hope the simple example makes it clear as to how to process command line arguments in C++.
Have no idea about your situation, but surely you will have realize what parameters do you really need?
If you just need arguments from the command line, simple use the char** argv variable. In complicated cases you can use GNU getopt or even Boost::Program_options (the last is cross-platform);
If you are trying to access environmental variables, use standard getenv functions.

C++: Unable to resolve identifier cout, Netbeans, Ubuntu

I am using C++ on Netbeans 7.1 on Ubuntu 11.04. For some reason, the following code results in the error message "Unable to resolve identifier cout".
#include <iostream>
using namespace std;
int main()
{
std::cout << "Hello,world!\n";
return 0;
}
Any help resolving this problem would be greatly appreciated.
The solution for your problem is at least strange ;)
Once iostream header is added, one has to reparse code. Click right on a project, go to code assistance and click to reparse project. Worked for me.
I was using netbeans for mac.
check whether iostream is really getting included;
i have tried your code on my machine using eclipse cdt it worked fine.so, please check the
includes.
What sort of file is this in? Is it a .h file, or .hpp file? I had this same issue. Netbeans can be ridiculous sometimes with C++. For me, I changed #include <iostream> to #include<iostream.h>
This may seem too simple, but...
In my NetBeans installation, when I go to create a new project, specify C/C++, it brings up a dialog box prompting for "Project Name:", location, folder, makefile name, and then...
a check box for "Create Main File", an edit box with "main" filled in, and to the right of that is a drop down list that reads "C". If you hit Finish, this will create "main.c" (C, but NOT a C++ file). Instead, in the drop down list, select "C++". Then the IDE creates main.cpp, which will be compiled with g++ and will find those includes and functions.
There is a difference between std::cout and cout. You don't currently have std::cout defined in your file. std::cout is a c standard out. In C++ we only need cout to work with iostream.
If you must use a standard c out then do the following:
Add this to the top under iostream
#include <iostream> //Input output stream in C++
#include <cstdlib> //Stands for c standard library
using namespace std;
Your code will now work because:
This change defines std::cout and std::cin among other things. (standard in, standard out respectively.)
However, I'd recommend this alternative if you don't need standard in outs:
Replace std::cout with cout, because cout is defined in iostream in C++. Your program would have worked without the std:: portion of your cin cout commands because you originally included iostream.
Try taking out the using namespace std; - it's generally considered bad form anyway :-)
I'm not sure that will fix the problem but most people either use the namespace or fully qualify things like std::cout. I've never seen code that does both.
The other thing to check is that the iostream header actually is being bought in. In other words, are there any errors on that line. A lot of problems (at least in the Windows world, so it may not necessarily apply to you) seem to be due to faulty path setup in NetBeans.
Hey look at your Output Debug. You may see "no permission". After I changed the file permission of "/YourProjekt/dist/Debug/GNU-Linux/file" to runable and everyone can read and write the error disappeared. (BTW: I had the bug because I was on a NTFS System with my Projekt, it have to be ext partition)
Hope I can help you with that.
Try taking out the std:: next to cout

Segmentation fault when different input is given

I do some image processing work in C++. For this i use CImg.h library which i feel is good for my work.
Here is small piece of code written by me which just reads an image and displays it.
#include "../CImg.h"
#include "iostream"
using namespace std;
using namespace cimg_library;
int main(int argc,char**argv)
{
CImg<unsigned char> img(argv[1]);
img.display();
return 0;
}
When i give lena.pgm as input this code it displays the image. Where as if i give some other image, for example ddnl.pgm which i present in the same directory i get "Segmentation Fault".
When i ran the code using gdb i get the output as follows:
Program received signal SIGSEGV, Segmentation fault.
0x009823a3 in strlen () from /lib/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.9-2.i686 libX11-1.1.4-5.fc10.i386 libXau-1.0.4-1.fc10.i386 libXdmcp-1.0.2-6.fc10.i386 libgcc-4.3.2-7.i386 libstdc++-4.3.2-7.i386 libxcb-1.1.91-5.fc10.i386
Can some one please tell me what the problem is? and how to solve it.
Thank you all
Segfault comes when you are trying to access memrory which you are not allowed to access.
So please check that out in the code.
The code itself looks just fine. I can suggest some ways to go ahead with debugging -
Try removing the display() call. Does the problem still occur? (I'd assume it does).
Try finding out where in the CImg code is the strlen() that causes the segmentation fault (by using a debugger). This may give additional hints.
If it is in the PGM file processing, maybe the provided PGM file is invalid in some way, and the library doesn't do error detection - try opening it in some other viewer, and saving it again (as PGM). If the new one works, comparing the two may reveal something.
Once you have more information, more can be said.
EDIT -
Looking at the extra information you provided, and consulting the code itself, it appears that CImg is failing when trying to check what kind of file you are opening.
The relevant line of code is -
if (!cimg::strcmp(ftype,"pnm")) load_pnm(filename);
This is the first time 'ftype' is used, which brings me to the conclusion that it has an invalid value.
'ftype' is being given a value just a few lines above -
const char *const ftype = cimg::file_type(0,filename);
The file_type() function itself tries to guess what file to open based on its header, probably because opening it based on the extension - failed. There is only one sane way for it to return an invalid value, which would later cause strcmp() to fail - when it fails to identify the file as anything it is familiar with, it returns NULL (0, actually).
So, I reiterate my suggestion that you try to verify that this is indeed a valid file. I can't point you at any tools that are capable of opening/saving PGM files, but I'm guessing a simple Google search would help. Try to open the file and re-save it as PGM.
Another "fun to track down" cause of segmentation faults is compilier mismatches between libraries - this is especially prevalent when using C++ libraries.
Things to check are:
Are you compiling with the same compiler as was used to compile the CImg library?
Are you using the same compiler flags?
Were there any defines that were set when compiling the library that you're not setting now?
Each of these has bitten me in subtle ways before.