【发布时间】:2023-04-10 20:34:01
【问题描述】:
我必须使用 C++ 和 VxWorks 将命令“ifconfig”的输出保存到字符缓冲区中。 我该怎么做?
【问题讨论】:
-
VxWorks没有
popen吗? -
请添加您尝试过的代码。请查看How to Ask 文章,了解如何让您的问题变得更好。
我必须使用 C++ 和 VxWorks 将命令“ifconfig”的输出保存到字符缓冲区中。 我该怎么做?
【问题讨论】:
popen吗?
ifconfig 是一个 shell 命令,因此您应该能够使用 '>' 将其输出重定向到一个文件,然后读取该文件。
您还可以查看手册中的“Redirecting Shell IO”主题。
【讨论】:
这是一个使用管道将 ifconfig 的输出保存到缓冲区的示例。
在 C 解释器 shell 上尝试 -> pipe_test 和 -> puts &pipe_buf。祝你好运。
char pipe_buf[128*256];
int pipe_test()
{
char *pipe_name = "pipe01";
int pipe_fd;
int out_fd;
int nlines;
int nbytes;
if (pipeDevCreate(pipe_name,128,256) == ERROR) { /* 128 lines of size 256 bytes */
perror("pipeDevCreate");
return -1;
}
if ((pipe_fd = open(pipe_name,O_RDWR,0666)) < 0) {
pipeDevDelete(pipe_name,TRUE);
perror("open");
return -1;
}
out_fd = ioTaskStdGet(0,STD_OUT);
ioTaskStdSet(0,STD_OUT,pipe_fd);
ipcom_run_cmd("ifconfig -a");
ioTaskStdSet(0,STD_OUT,out_fd);
if (ioctl(pipe_fd, FIONMSGS, &nlines) == OK && nlines > 0) {
char *pbuf = &pipe_buf[0];
int ln;
memset(pipe_buf,0,sizeof(pipe_buf));
for (ln=0; ln<nlines && ln<128; ln++) {
nbytes = read(pipe_fd,pbuf,256);
pbuf += nbytes;
}
}
close(pipe_fd);
pipeDevDelete(pipe_name,TRUE);
return 0;
}
【讨论】: