I have a problem with reading a text file in c++ and I can't figure out what is wrong. I need to read few complex numbers from file and do some calculations using matlab functions. So before that I compiled matlab library and included them to my project. I tested them and it works correctly. But when I added a few lines of code for reading data from file I get some errors:
Here is my code:
#include "spectrum.h"
#include <iostream>
#include <fstream>
#include <complex>
using namespace std;
int main(){
mclInitializeApplication(NULL,0);
spectrumInitialize();
cout<<"Initialization success"<<endl;
double input[4] = {10,20,30,40};
mxArray *x_ptr, *x_ptr1;
mxArray *y_ptr=NULL;
mxArray *y_ptr1=NULL;
double *y;
double *y1;
complex<double> com[256];
ifstream myfile("dep.txt");
double a = NULL;
for(int i=0; i<256;i++)
{
myfile >> a;
com[i].real(a);
myfile >> a;
com[i].imag(a);
}
myfile.close();
x_ptr1 = mxCreateDoubleScalar(256);
x_ptr = mxCreateDoubleMatrix(1,256,mxCOMPLEX);
memcpy((void *)mxGetPr(x_ptr), (void *) com,256*sizeof(complex<double>));
mlfSpectrum_slice(1,&y_ptr,x_ptr,x_ptr1);
y = (double*)mxGetPr(y_ptr);
cout<<"Data: "<<input<<endl;
cout<<"Result: "<<*y<<endl;
mxDestroyArray(x_ptr);
mxDestroyArray(y_ptr);
spectrumTerminate();
mclTerminateApplication();
return 0;
}
I tried to use fstream and ifstream too. Anybody knows what I'm doing wrong?
By the way, I'm using Windows 7 32bit, Visual studio 2012 and Matlab R2012b.
You have a linkage problem. The linker can't find the function CrtDbgReport. This is a Microsoft debug function. It is all about writing debug messages using OutputDebugString API. Microsoft lib is kernel32.lib - so have you linked with that? Specifically it is looking for the Unicode version, that's the W on the end: CrtDbgReportW. So you must have set a Unicode build somewhere. ie #define UNICODE.
Is your lib maybe NOT using Unicode.
Another possibility is you are linking against debug matlab lib but you are building a release version of your program. Or vice versa.
Related
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream archivo("saludos.txt",ios::in|ios::out); // Abrir y Leer
char caract;
int cont=0;
while (!archivo.eof())
{
archivo.seekg(cont,ios::beg);
caract=archivo.get();
if (caract=='A')
{
archivo.seekp(cont,ios::beg);
archivo<< 'O';
}
cont++;
}
archivo.close ();
}
i'm using this code but when i build and run it, nothing happens in the file saludos.txt There isn't even a response on the console application. anybody know why? i'm using codeblocks and i also have #include fstream
I tried your code in my environment and it works just fine, it replace all 'A' characters with '0'.
I think your problem is with Codeblocks, if you create a new project and want to use file with the relative path in your project, you have to go to Project -> Properties -> Build targets and change the "Executing Working Dir" to your project's folder.
Or you cant try with a absolute file name with full system path first.
I'm trying to load pngs or bmps into my programs. But none of the libraries I found around the web works. I have no idea why but I always get "incorrect PNG signature, it's no PNG or corrupted" for EVERY png when using lodepng. "unknown pixel format" for EVERY bmp when using SDL_loadBMP. "unknown image type" for every png when using stb_image.
I can't load anything. Maybe there is something wrong with my system ? I'm using OSX Yosemite. Here is the code.
#include <iostream>
#include <string>
#define STB_IMAGE_IMPLEMENTATION
#include <lodepng.h>
#include <stb_image.h>
using namespace std;
int main (){
string name = "res/img_test.png";
const char * cstr = name.c_str();
//lodepng
unsigned char *buffer;
unsigned int w,h;
int result = lodepng_decode32_file(&buffer, &w, &h, cstr);
cout << lodepng_error_text(result) << endl;
//stb_image
int x, y, comp;
FILE *f = fopen(cstr, "rb");
unsigned char *res;
res = stbi_load_from_file(f,&x,&y,&comp,0);
fclose(f);
cout << stbi_failure_reason() << endl;
return 0;
}
I'm using latest cmake to build this with gcc. Any recommendatation is appreciated but consider this. I've tried many files (generated by me or grabbed from internet). Tested same files with other users of the these libraries. Their code worked and mine didn't.
Edit:
Here's the source with complete cmake project >> github.com/onatbas/png_load_test.git
I solved it! Thank you everyone for trying to help.
It wasn't a code issue, afterall. It is a configuration issue. My cmake script damages the pngs and bmps when trying to copy them into destination folder. The code is fine.
After testing my code alone, I decided to incorporate it into the project finally. The issue is, when LabVIEW 2010 (SP1, 64-bit) loads the custom DLL, it walks the dependencies and finds that it needs tbb.dll eventually. Well, as far as I can tell, LabVIEW uses its own version of tbb.dll. And its version is missing the entry point ?throw_exception_v4#internal#tbb##YAXW4exception_id#12##Z. I ran the function before separately, and it worked fine. It would appear this is not an unheard of LabVIEW error which hasn't been addressed.
Since this is a dependency of a third party library, I can either not use the MATLAB libraries and make everything from scratch, or I can force LabVIEW to load the working version of tbb.dll, which appears to mean copying the DLL into the Windows folder. Neither of these are usable solutions. Additionally, we do not have the license for Mathscript. Any ideas?
Just in case I did something wrong when I modified their example, a simplified version of my code is as follows:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include "mat.h"
#include "createMatFile.h"
export "C" int createMatFile(const char *filename, int* dataArray, int count)
{
MATFile *pmat;
mxArray *data;
mwSize dims[2] = {1,1};
//Open the file to write to
pmat = matOpen(filename, "w");
if (pmat == NULL) {
printf("Error creating file %s\n", filename);
printf("(Do you have write permission in this directory?)\n");
return(EXIT_FAILURE);
}
//Convert data to double
double* dataDouble = new double[count];
for(int i=0; i<count; i++)
{
dataDouble[i] = (double)data[i];
}
//Populate the mxArrays
dataArray = mxCreateDoubleMatrix(1,count,mxREAL);
memcpy((void *)(mxGetPr(dataArray)), (void *)dataDouble, sizeof(*dataDouble)*count);
//Put the shape struct in the .mat file
int status = matPutVariable(pmat, "data", dataArray);
if (status != 0) {
printf("%s : Error using matPutVariable on line %d\n", __FILE__, __LINE__);
return(EXIT_FAILURE);
}
//Clean up
delete [] dataDouble;
mxDestroyArray(dataArray);
//Close the file to write to
if (matClose(pmat) != 0) {
printf("Error closing file %s\n",filename);
return(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
The .h file is just the prototype of the function.
Because LabVIEW installs its own version of tbb.dll into the windows path, and loads that, the only way to fix it is to copy the new tbb.dll in the system32 folder. This is not a good solution, so the only answer possible appears to be either:
Use this system32 hack which will probably cause other issues down the road.
Create some hack to dynamically load the proper DLL in a separate process with nothing loaded from LabVIEW and call the MATLAB DLL from that process then return the value computed to the LabVIEW process. Since my program only writes data to a file, it doesn't have to return anything back to LabVIEW, simplifying this choice.
Or just stop using MATLAB and LabVIEW in conjunction.
I coded the below which tries to hold a picture and count number hundred and finally grab all them to a video with following codec MP42(MPEG4-V2)
#include <stdAfx.h>
#include <iostream>
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int n=0;
char a;
int main(int, char** argv)
{
Mat image= imread("C:\\Users\\Metin\\Desktop\\images.jpg");
Mat newimage;
VideoWriter vide ("Hopdedik.avi",CV_FOURCC('M','P','4','2'),30.0,image.size());
vide.open("Hopdedik.avi",CV_FOURCC('M','P','4','2'),30.0,image.size());
for(int n=0; n<100;n++){
cout<<n<<'\n';
std::string s= to_string(static_cast<long long>(n));
image.copyTo(newimage);
putText(newimage,s,Point(100,40),cv::FONT_HERSHEY_COMPLEX,2,cv::COLORMAP_COOL,2,2,false);
imshow("asd",newimage);
if(!vide.isOpened())cout<<"can not opened";
vide.write(newimage);
waitKey(10);
}
vide.release();
return 0;
}
I started debugging on Release mode and copied the .exe files to Virtualbox Win7-64 bit. Right after I put necessary .dll files exe works but the code can't properly create the video.
I have tried to install following two codecs:
http://www.fourcc.org/downloads/angel-potion-mpeg-4--8/
and Indeo which is: http://www.afreecodec.com/codec-by-code/iv45-160.html
But it did not work.
Once I faced a similar problem where opencv couldn't read a video. There was no warning at all. The reason was that an automated string maker resulted in "opencv_mmpeg.dll" which was missing from the system. So, I debugged up to cap_ffmpeg.cpp and found this:
#if defined WIN32 || defined _WIN32
const char* module_name = "opencv_ffmpeg"
CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) CVAUX_STR(CV_SUBMINOR_VERSION)
#if (defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__)
"_64"
#endif
".dll";
So, copy opencv_ffmpeg_64.dll to your folder and you might have some results. Else, you need to debug deeply. Start by inserting into your project the file opencv/modules/highgui/src/cap.cpp (and the headers needed to be compiled) so you can debug line by line. I suppose many other files will be needed to go further down into the code.
i have following code
#include <iostream>
using namespace std;
int main(int argc,char arg[]){
int a=arg[1];
int b=arg[2];
int c=a+b;
cout<<c<<endl;
return 0;
}
i am using windows 7 microsoft visual c++ 2010
how run it from command line?
Open a command prompt from the Start Menu. Use the CD command to change directories to where your exe is. type the name of your exe followed by the arguments.
foo.exe 1 2
or just
foo 1 2
Expect the output (once you've fixed your numerous code errors):
3
Once you compile this you get an executable. Navigate to the directory containing the executable and run it.
Go to google and look for a windows console tutorial. You need to start it from the console. Alternatively you can assign command line in the project properties. I'd recommend learning to do both.
BTW, this code almost certainly does not do what you think it does.
The compiled output of your program will be in the Debug or Release folder inside the solution folder (at least with default project settings). Just change to that directory and run the .exe file.
Open the Visual Studio Command Prompt (you can find it in the Start Menu)
cd to your source file directory
type:
cl.exe <your file name>.cpp
It will create a file .exe
Once your code is setup properly it would be something like this.
MyApp 2 3
Or similar
Navigate to the directory where the executable (.exe) is located. Then type the executable's name followed by two integer parameters.
C:\TestProg\> TestProg 5 6
The problems in your original example are corrected here:
#include <iostream>
#include <sstream>
int main(int argc, char *arg[])
{
std::stringstream sa;
std::stringstream sb;
int a;
int b;
int c;
if (argc >= 3)
{
// Convert string parameter into an integer.
sa.str(arg[1]);
sa >> a;
if (!sa)
{
return 1; // error
}
// Convert string parameter into an integer.
sb.str(arg[2]);
sb >> b;
if (!sb)
{
return 1; // error
}
}
else
{
return 1; // error
}
c = a + b;
std::cout << c << std::endl;
return 0;
}