gethostbyaddr() with segmentation fault, why? - c++

I have a c++ program to get the local machine name using gethostbyaddr() method, the requirement is to run the program with argument which is an ip address.
#include <iostream>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include <cstdlib>
using namespace std;
int main(int argc, char const *argv[]) {
struct hostent * testing_case;
struct in_addr ipv4addr;
if (argc != 2) {
cout<<"Error"<<endl;
return 0;
}
else{
inet_aton(argv[1],&ipv4addr);
testing_case = gethostbyaddr(&ipv4addr, sizeof(ipv4addr), AF_INET);
cout<<"Host Name is: "<<testing_case->h_name<<endl;
}
return 0;
}
But when I input the local ip address it returns me a segmentation fault, please tell me how to fix it?

Related

command spawnl() not executed

I have a code that is executable without error messages but it seems like it denies to run the code
I call by the "spawnl" command. This is my code and I receive "error=-1". I tried may different ways to solve the situation but I always receive the "-1" for an answer. I use Dev C++ compiler with 32bit release. My problem is to call the other program, sending the name of the file.
#include <stdlib.h>
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <conio.h>
#include <process.h>
#include <ctype.h>
#include <dir.h>
#include <windows.h>
#include <dos.h>
#include <iostream>
#include <stddef.h>
void translate_to_ascii_files(),
save_mesh_data(),
make_no(),
give_names();
int load_patches(char *);
FILE *memco ;
int outnod[400] ;
float r_vector[21][21][3],cx[500],cy[500],cz[500] ;
int NOP1,NOP2; /* Number Of Points */
int patches;
char nams[26][26];
int num_of_files;
int kk,nv,nh,exnod,totnod ;
int no[4][400];
char filename[26];
void *buf;
COORD coord= {0,0};
HANDLE hConsole;
using namespace std; // std::cout, std::cin
void gotoxy(int x,int y) {
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
int main(int argc,char *argv[]) {
char com_nam[26];
int counter1,counter2,i;
int error;
system("cls");
give_names();
printf("give the final filename:");scanf("%s",filename);
for(patches=1;patches<=num_of_files;patches++) {
strcpy(com_nam,nams[patches]);
load_patches(com_nam); /* load patches for meshing*/
i=1;
for(counter1=0;counter1<=NOP1;counter1++) {
for(counter2=0;counter2<=NOP2;counter2++) {
cx[i]=r_vector[counter1][counter2][0];
cy[i]=r_vector[counter1][counter2][1];
cz[i]=r_vector[counter1][counter2][2];
i=i+1;
}
}
nh=NOP1+1;nv=NOP2+1;
make_no();
save_mesh_data();
if(argc ==1){
error=spawnl(P_WAIT,"c:\\cpprog\\unitsrf.exe","",filename,NULL);
if (error ==0) {
else {printf ("error=%d\n",error); system("PAUSE");
} }
else{gotoxy(2,11);printf("error=%s\n\n",argv[1]);
system("PAUSE");
}
} translate_to_ascii_files();
}

How to communicate to a gsm modem in c++ using IOKit?

I am trying to create a program in c++ on xcode to communicate to the gsm modem and send sms through AT comands. I haven' t found much on the net. Here is the code that I have written till now.
#include <iostream>
#include <xlnt/xlnt.hpp>
#include <xlnt/xlnt_config.hpp>
#include <IOKitLib.h>
#include <IOTypes.h>
#include <IOReturn.h>
#include <IOKitKeys.h>
#include <iokitmig.h>
#include <OSMessageNotification.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <paths.h>
#include <termios.h>
#include <sysexits.h>
#include <sys/param.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/serial/IOSerialKeys.h>
#include <IOKit/IOBSD.h>
#define LOCAL_ECHO
#ifdef LOCAL_ECHO
#define kOKResponseString “AT\r\r\nOK\r\n”
#else
#define kOKResponseString “\r\nOK\r\n”
#define kATCommandString "AT\r"
#define kMyErrReturn -1
enum {
kNumRetries = 3
};
static struct termios gOriginalTTYAttrs;
int main()
{
char path;
char text;
int fileDescriptor;
kern_return_t kernResult;
io_iterator_t serialPortIterator;
char deviceFilePath[MAXPATHLEN];
kernResult = MyFindModems(&serialPortIterator);
kernResult = MyGetModemPath(serialPortIterator, deviceFilePath,
sizeof(deviceFilePath));
IOObjectRelease(serialPortIterator); // Release the iterator.
// Open the modem port, initialize the modem, then close it.
if (!deviceFilePath[0])
{
printf("No modem port found.\n");
return EX_UNAVAILABLE;
}
fileDescriptor = OpenSerialPort(deviceFilePath);
if (fileDescriptor == kMyErrReturn)
{
return EX_IOERR;
}
if (MyInitializeModem(fileDescriptor))
{
printf("Modem initialized successfully.\n");
}
else {
printf("Could not initialize modem.\n");
}
MyCloseSerialPort(fileDescriptor);
printf("Modem port closed.\n");
return EX_OK;
It's giving me an error "use of undeclared identifier MyFindModems" and all the other fuctions that start with "My".
Here is the documentation to which I refer: http://mirror.informatimago.com/next/developer.apple.com/documentation/DeviceDrivers/Conceptual/WorkingWSerial/WorkingWithSerial.pdf

Unable to conversion in Expected<unique_ptr<Module>> to Module

I am new to llvm , writing the program "main.cpp" of https://github.com/davidar/lljvm/blob/master/backend/main.cpp.
I stuck at the error while executing the command : "pm.run(m)"
error: no matching function for call to ‘llvm::legacy::PassManager::run(llvm::Expected<std::unique_ptr<llvm::Module> >&)
Here is my source code:
#include "backened.h"
#include <iostream>
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/PassManager.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/IR/DataLayout.h"
#include <llvm/Transforms/Scalar.h>
#include "llvm/IR/LegacyPassManager.h"
using namespace llvm;
using namespace std;
static cl::opt<string> input(cl::Positional, cl::desc("Bitcode File.."),cl::Required);
static cl::opt<string> classname("classname",cl::desc("Binary name of the generated class..."));
int main(int argc, char** argv)
{
cl::ParseCommandLineOptions(argc, argv, "Hi..");
LLVMContext context;
ErrorOr<unique_ptr<MemoryBuffer>> mb = MemoryBuffer::getFile(input);
if(error_code ec = mb.getError()) {
errs() << ec.message();
return 1;
}
Expected<unique_ptr<Module>> m = parseBitcodeFile(mb->get()->getMemBufferRef(),context);
if(error_code ec= errorToErrorCode(m.takeError()) )
{
errs() <<"Unable to read bitcode file.." <<ec.message() ;
}
PassManager<Module> pm;
pm.add(createVerifierPass());
pm.add(createGCLoweringPass());
pm.add(createLowerSwitchPass());
pm.add(createCFGSimplificationPass());
pm.add(new JVMWriter(fouts(), classname, debugLevel));
pm.add(createGCInfoDeleter());
pm.run(*m);
return 0;
}
Please help me.

C++ regex cstring error

I wrote this code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <regex>
using namespace std;
int compile()
{
string linecode;
linecode = "_hello:";
if (std::regex_match (linecode, std::regex("(_)(.*)(:)") ))
{
cout<<"...";
}
else if (std::regex_match (linecode, std::regex("(#)(.*)(:)") ))
{
cout<<"###";
}
return 1;
}
int main(int argc, char** argv)
{
compile();
return 0;
}
And i get this error:
enter image description here
cstring error, probably in regex library.
How i can fix this ?
Thanks in advance.

Loading an image from a folder using OpenCV 3.0 in Windows

I am using Visual Studio 2010, with OpenCV 3.0. I'm trying to load some images from a folder but I am having problems.
Firstly I did not have the file dirent.h, so I downloaded it in order to get the DIR* and "dirent*" structures to access to the files. All seems to be well, but now when I get to the line
string fileName = in_file->d_name;
I have found that I don't access to the name of the file.
Anyone have any thoughts on this?
This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <opencv2/core/dirent.h>
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include <conio.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#pragma comment(lib, "User32.lib")
#include <errno.h>
#include <iostream>
#include <time.h>
#include <limits.h>
#include <fstream>
#include <sys/stat.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace cv::ml;
using namespace std;
int patchWidth = 15;
int patchHeight = 15;
int main(int, char**)
{
string imagesPath = "Images";
string resultsPath = "Patches";
DIR* FD;
struct dirent* in_file;
if (NULL == (FD = opendir (imagesPath.c_str())))
{
fprintf(stderr, "Error : Failed to open input directory\n");
return 0;
}
while ((in_file = readdir(FD)))
{
/* On linux/Unix we don't want current and parent directories
* If you're on Windows machine remove this two lines
*/
// if (!strcmp (in_file->d_name, "."))
// continue;
// if (!strcmp (in_file->d_name, ".."))
// continue;
/* Open directory entry file for common operation */
/* TODO : change permissions to meet your need! */
string fileName = in_file->d_name;
string pathFile = imagesPath;
pathFile.append("/");
pathFile.append(fileName);
//pathFile.append(".jpg");
Mat img = imread(pathFile.c_str());
Thanks in advance.
for a much simpler solution, just use cv::glob :
String imagesPath = "Images/*.png"; // it has filters, too !
vector<String> fn;
glob(path, fn, true); // recursive, if you want
for (size_t i=0; i<fn.size(); i++)
{
Mat img = imread(fn[i]);
...
}