[英]How to copy the output of linux command to a C++ variable
I'm calling a LINUX command from within a C++ programme which creates the following output. 我正在从C ++程序中调用LINUX命令,该程序创建以下输出。 I need to copy the first column of the output to a C++ variable (say a long int). 我需要将输出的第一列复制到C ++变量(例如long int)。 How can I do it?? 我该怎么做?? If that is not possible how can I copy this result into a .txt file with which I can work with? 如果不可能,如何将结果复制到可以使用的.txt文件中?
Edit 编辑
0 +0
2361294848 +2361294848
2411626496 +50331648
2545844224 +134217728
2713616384 +167772160
I have this stored as a file, file.txt and I'm using the following code to extract the left column with out the 0 to store it at integers 我将其存储为文件file.txt,并使用以下代码提取左列(不包含0)以将其存储为整数
string stringy="";
int can_can=0;
for(i=begin;i<length;i++)
{
if (buffer[i]==' ' && can_can ==1) //**buffer** is the whole text file read in char*
{
num=atoi(stringy.c_str());
array[univ]=num; // This where I store the values.
univ+=1;
can_can=1;
}
else if (buffer[i]==' ' && can_can ==0)
{
stringy="";
}
else if (buffer[i]=='+')
{can_can=0;}
else{stringy.append(buffer[i]);}
}
I'm getting a segmentation error for this. 我为此遇到了细分错误。 What can be done ? 该怎么办?
Thanks in advance. 提前致谢。
Just create a simple streambuf wrapper around popen() 只需围绕popen()创建一个简单的streambuf包装器
#include <iostream>
#include <stdio.h>
struct SimpleBuffer: public std::streambuf
{
typedef std::streambuf::traits_type traits;
typedef traits::int_type int_type;
SimpleBuffer(std::string const& command)
: stream(popen(command.c_str(), "r"))
{
this->setg(&c[0], &c[0], &c[0]);
this->setp(0, 0);
}
~SimpleBuffer()
{
if (stream != NULL)
{
fclose(stream);
}
}
virtual int_type underflow()
{
std::size_t size = fread(c, 1, 100, stream);
this->setg(&c[0], &c[0], &c[size]);
return size == 0 ? EOF : *c;
}
private:
FILE* stream;
char c[100];
};
Usage: 用法:
int main()
{
SimpleBuffer buffer("echo 55 hi there Loki");
std::istream command(&buffer);
int value;
command >> value;
std::string line;
std::getline(command, line);
std::cout << "Got int(" << value << ") String (" << line << ")\n";
}
Result: 结果:
> ./a.out
Got int(55) String ( hi there Loki)
It is popen
you're probably looking for. 您可能正在寻找的popen
。 Try 尝试
man popen
. 。
Or see this little example: 或者看这个小例子:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
FILE *in;
char buff[512];
if(!(in = popen("my_script_from_command_line", "r"))){
return 1;
}
while(fgets(buff, sizeof(buff), in)!=NULL){
cout << buff; // here you have each line
// of the output of your script in buff
}
pclose(in);
return 0;
}
You probably want to use popen
to execute the command. 您可能想使用popen
执行命令。 This will give you a FILE *
that you can read its output from. 这将为您提供一个FILE *
,您可以从中读取其输出。 From there, you can parse out the first number with (for example) something like: 从那里,您可以使用以下内容解析出第一个数字:
fscanf(inpipe, "%d %*d", &first_num);
which, just like when reading from a file, you'll normally repeat until you receive an end of file indication, such as: 就像从文件中读取时一样,通常会重复执行直到收到文件结束指示,例如:
long total = 0;
while (1 == fscanf(inpipe, "%l %*d", &first_num))
total = first_num;
printf("%l\n", total);
Unfortunately, it's not easy since the platform API is written for C. The following is a simple working example: 不幸的是,由于平台API是为C编写的,所以这并不容易。以下是一个简单的工作示例:
#include <cstdio>
#include <iostream>
int main() {
char const* command = "ls -l";
FILE* fpipe = popen(command, "r");
if (not fpipe) {
std::cerr << "Unable to execute commmand\n";
return EXIT_FAILURE;
}
char buffer[256];
while (std::fgets(buffer, sizeof buffer, fpipe)) {
std::cout << buffer;
}
pclose(fpipe);
}
However, I'd suggest wrapping the FILE*
handle in a RAII class to take care of resource management. 但是,我建议将FILE*
句柄包装在RAII类中以进行资源管理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.