为什么popn()一次又一次调用返回相同的输出
why same output is returned when popn() is calling again and again
我正在尝试下面的例子
#include <boost/thread.hpp>
#include <boost/thread/condition.hpp>
#include <iostream>
#include <string>
#include <stdio.h>
std::string exec(char* cmd, boost::uint16_t *piOutVid, boost::uint16_t *piOutPid) {
boost::uint16_t uint_pid;
boost::uint16_t uint_vid = 0x05e0;
piOutVid = &uint_vid;
FILE* pipe1 = popen(cmd, "r");
if (!pipe1) return "ERROR";
char buffer[128];
unsigned int value;
std::string result = "";
while(!feof(pipe1)) {
if(fgets(buffer, 128, pipe1) != NULL)
if(strncpy(buffer, "1300", 4))
{
uint_pid = 0x1300;
std::cout << "value: " << buffer << std::endl;
}
else if(strncpy(buffer, "1900", 4))
{
uint_pid = 0x1900;
std::cout << "value: " << buffer << std::endl;
}
else if(strncpy(buffer, "0820", 4))
{
uint_pid = 0x0820;
std::cout << "value: " << buffer << std::endl;
}
result += buffer;
}
pclose(pipe1);
buffer[127] = ' ';
return result;
}
int main()
{
boost::uint16_t *piOutVid;
boost::uint16_t *piOutPid;
std::cout << "Boost threading..." << std::endl;
//boost::thread *nwThread = new boost::thread(boost::bind(class::method, this));
char *cmd = "lsusb|grep 'Symbol'|cut -d ":" -f 3|cut -d " " -f 1";
exec(cmd, piOutVid, piOutPid);
}
在第一次运行这个程序时。它工作正常,但重用程序不能正常工作。我第一次用设备(PID = 1300)进行测试,然后将其删除并插入新设备(PID = 0820)并再次运行程序。但仍然给出下面相同的输出。
Boost threading...
value: 1300
Process returned 0 (0x0) execution time : 0.059 s
Press ENTER to continue.
pclose()
返回0
您的if
语句是基于strncpy
的结果,它总是返回destination
的内存地址。所以根据定义,如果复制成功,它总是会返回一个计算结果为true
的值。因此总是击中你的0x1300
块。
你是想用strcmp
吗?
相关文章:
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 返回 str vs. str.substr(0,str.size()) 在 leetcode 中给了我不同的输出
- C++输出参数与返回值
- cpp 返回具有相同输入的不同输出
- 显示C++输出而不返回值
- 简单函数并不总是返回预期的输出
- 输出返回编号。等于输入字符串的单词的字符串数
- 如何在C++中执行命令并获取命令的返回代码标准输出和标准
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- 为什么 boost::locale::to_title 不返回预期的输出?
- 在函数中连接两个字符,并在 C++ 中返回输出
- 同一个程序每次返回不同的输出
- C++如果使用 lambda 表达式的语句返回 true,但输出来自 false,为什么
- cout.width和cout.fill返回不同的输出
- C 输出返回值
- 面向对象C++帮助?出于某种原因,对象变量的输出返回数值数组位置而不是实际值
- 调用一个方法会将输出返回到同一类c++的另一个方法中
- 二进制文件中结构的输出返回垃圾
- KissFFT输出返回一个值
- 为什么FFTW输入非零,输出返回全零(c++)