【问题标题】:why same output is returned when popn() is calling again and again [closed]为什么当popn()一次又一次调用时返回相同的输出[关闭]
【发布时间】:2014-08-01 13:19:31
【问题描述】:

我正在尝试下面的示例

#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] = '\0';


    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

【问题讨论】:

  • 您将strncmp 拼错为strncpy
  • 请删除您的问题;它不是基于愚蠢的错字,因此对未来的访问者没有任何价值。

标签: c++ popen pclose


【解决方案1】:

您的if 语句基于strncpy 的结果,它始终返回destination 的内存地址。所以根据定义,如果复制成功,它总是会返回一个评估为true 的值。因此总是点击你的0x1300 块。

您的意思是使用strcmp

【讨论】:

    猜你喜欢
    • 2021-07-24
    • 2021-03-30
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 2015-09-15
    • 1970-01-01
    相关资源
    最近更新 更多