gpt4 book ai didi

c++ - 使用反引号时程序不输出

转载 作者:太空宇宙 更新时间:2023-11-04 05:28:42 29 4
gpt4 key购买 nike

我希望将 detectUpdateHomeML 的输出作为命令行参数传递到名为 insertElement 的程序中。

当我尝试使用命令时

./insertElement  `./detectUpdateHomeML`

程序detectUpdateHomeML不再输出到控制台,insertElement也不再向XML文件插入任何内容。

当我用测试输出程序替换 detectUpdateHomeML 时,一切都会按预期工作,输出程序输出,并且 insertElement 将输出的信息插入到 XML 中。

为什么它不能与 detectUpdateHomeML 一起使用?

检测UpdateHomeML输出:

            std::cout << eventCount << " " << cascadeName << " " << timeFound();

std::cout << std::flush;

int timeFound() {
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (buffer,80," %Y-%m-%dT%H:%M:%S",timeinfo);
puts (buffer);
eventCount++;
}

插入元素代码

   #include "tinyxml2.h"
#include <iostream>

using namespace tinyxml2;

int main( int argc, const char* argv[] ) {

XMLDocument doc;
doc.LoadFile("homeML.xml");// Gets the root <homeML> element
XMLElement *pRoot = doc.FirstChildElement("homeML");



/********************************************************************************************************************************************************************************
* Adds new startTimeStamp to <realTimeInformation> -> <startTimeStamp>.
*
* TODO - data value is hard coded - it needs to enter the first time stamp recorded of that instance of object recognition.
*
********************************************************************************************************************************************************************************/
XMLElement *pStartTimeStamp = pRoot->FirstChildElement("mobileDevice")->FirstChildElement("realTimeInformation");
XMLElement *pRealtimeStore = nullptr;
XMLElement *pIter0 = pStartTimeStamp->FirstChildElement("runID");
while (pIter0 != nullptr) {
pRealtimeStore = pIter0;
pIter0 = pIter0->NextSiblingElement("startTimeStamp");
}

if (pRealtimeStore != nullptr) {
XMLElement *pNewStartTimeStamp = doc.NewElement("startTimeStamp");
pNewStartTimeStamp->SetText("pNewTimeStamp");
pStartTimeStamp->InsertAfterChild(pRealtimeStore, pNewStartTimeStamp);

}

/********************************************************************************************************************************************************************************
* Adds new mEventID to <event> -> <mEventID>
*
* It takes in the first argument pass to the program.
*
********************************************************************************************************************************************************************************/
XMLElement *pmEventID = pRoot->FirstChildElement("mobileDevice")->FirstChildElement("event");
XMLElement *pmEventIDStore = nullptr;
XMLElement *pIter2 = pmEventID->FirstChildElement("mEventID");
while (pIter2 != nullptr) {
pmEventIDStore = pIter2;
pIter2 = pIter2->NextSiblingElement("mEventID");
}
if (pmEventIDStore != nullptr) {
XMLElement *pNewmEventID = doc.NewElement("mEventID");
pNewmEventID->SetText(argv[1]);
pmEventID->InsertAfterChild(pmEventIDStore, pNewmEventID);
}

/********************************************************************************************************************************************************************************
* Adds new timeStamp to <event> -> <timeStamp>.
*
* It takes in the third argument passed to the program.
*
********************************************************************************************************************************************************************************/
XMLElement *pEvent = pRoot->FirstChildElement("mobileDevice")->FirstChildElement("event");// Gets the <event> element
XMLElement *pPrecedent = nullptr;// This is to store the element after which will insert the new element <timeStamp>
XMLElement *pIter = pEvent->FirstChildElement("mEventID");// Gets the first location immediately before where a <timeStamp> element should be placed
while (pIter != nullptr) {// Loops through the children of <event> and finds the last <timeStamp> element
pPrecedent = pIter;// Store pIter as the best known location for the new <timeStamp> element
pIter = pIter->NextSiblingElement("timeStamp");// Attempt to find the next <timeStamp> element
}
if (pPrecedent != nullptr) {
XMLElement *pNewTimeStamp = doc.NewElement("timeStamp");// Create a new <timeStamp> element
pNewTimeStamp->SetText(argv[3]);
pEvent->InsertAfterChild(pPrecedent, pNewTimeStamp);// Insert the new element into the document
}

/********************************************************************************************************************************************************************************
* Adds new data to <event> -> <data>.
*
* It takes in the second argument passed to the program.
*
********************************************************************************************************************************************************************************/
XMLElement *pEventData = pRoot->FirstChildElement("mobileDevice")->FirstChildElement("event");
XMLElement *pEventDataStore = nullptr;
XMLElement *pIter1 = pEventData->FirstChildElement("timeStamp");
while (pIter1 != nullptr) {
pEventDataStore = pIter1;
pIter1 = pIter1->NextSiblingElement("data");
}

if (pEventDataStore != nullptr) {
XMLElement *pNewDataEvent = doc.NewElement("data");
pNewDataEvent->SetText(argv[2]);
pEventData->InsertAfterChild(pEventDataStore, pNewDataEvent);

}






doc.SaveFile("homeML.xml");
}

测试输入.cpp

#include <iostream>
int main()
{using namespace std;
std::cout << "Hello World!" << " ";
std:cout << "after space";
return 0;
}

最佳答案

当您在反引号中运行程序时,shell 会捕获其标准输出并在命令行上替换它。所以,你实际上正在执行

./insertElement {... whatever the output of ./detectUpdateHomeML is ...}

既然输出被捕获了,它自然不会显示。尝试运行

OUTPUT=`./detectUpdateHomeML`
echo "output is: $OUTPUT"
./insertElement $OUTPUT

(或者只是set -x)来查看实际发生的情况。

<小时/>

哦,这似乎只是一个 shell 问题 - 输出本身比这里的 C++ 代码更有帮助。

关于c++ - 使用反引号时程序不输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26406565/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com