#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
using namespace std;
string get_ttyUSBn(string name)
{
FILE *stream;
char buf[1024];
memset(buf, '\0', sizeof(buf));
string strcmd= "sh ~/get_ttyUSBnbyName.sh ";
strcmd+=name;
//cout<<"shell cmd: "<<strcmd<<endl;
const char *chcmd= strcmd.data();
stream = popen( chcmd,"r");
fread(buf,sizeof(char), sizeof(buf),stream);
pclose( stream );
string tmp(buf);
return tmp;
}
int main()
{
string strresult=get_ttyUSBn("mySmartUSB");
//const char *chres= strresult.data();
//printf("%s",chres);
//cout<<strlen(chres)<<endl;
cout<<strresult;
return 0;
}
介绍:
NAME
popen, pclose - pipe stream to or from a process
SYNOPSIS
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
popen(), pclose():
_POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE