首页 \ 问答 \ 如何使用POSIX执行命令并获取C ++中的命令输出?(How do I execute a command and get output of command within C++ using POSIX?)

如何使用POSIX执行命令并获取C ++中的命令输出?(How do I execute a command and get output of command within C++ using POSIX?)

当我从C ++程序中运行时,我正在寻找一种获取命令输出的方法。 我已经看过使用了system()函数,但这只会执行一个命令。 以下是我正在寻找的例子:

std::string result = system( "./some_command" ) ;

我需要运行一个任意命令并得到它的输出。 我看过Boost.org,但我没有找到任何能给我我需要的东西。


I am looking for a way to get the output of a command when it is run from within a C++ program. I have looked at using the system() function, but that will just execute a command. Here's an example of what I'm looking for:

std::string result = system("./some_command");

I need to run an arbitrary command and get its output. I've looked at Boost.org, but I have not found anything that will give me what I need.


原文:https://stackoverflow.com/questions/478898
更新时间:2022-06-27 15:06

最满意答案

看来罪魁祸首是执行IL编织的第三方库(Fody.PropertyChanged)。

如果我没有这个库创建一个新的WPF项目,我可以在Watch窗口中使用Math.Sin() 。 一旦我安装PropertyChanged ,我开始得到“在这种情况下不可用”的错误。

感谢@BACON带领我找到正确的道路。

以下是我在PropertyChanged网站上发布的问题的链接:

http://code.google.com/p/propertychanged/issues/detail?id=6&thanks=6&ts=1330494634

编辑

随后,事实证明这可能是Mono-Cecil中的一个错误,PropertyChanged使用这个错误:

https://github.com/jbevain/cecil/issues/90


It appears that the culprit is a third-party library that performs IL weaving (Fody.PropertyChanged).

If I create a new WPF project without this library, I'm able to use Math.Sin() in the Watch window. Once I install PropertyChanged, I start getting the "not available in this context" error.

Thanks to @BACON for leading me down the right path to figuring this out.

Here's a link to an issue I posted on the PropertyChanged site:

http://code.google.com/p/propertychanged/issues/detail?id=6&thanks=6&ts=1330494634

Edit

And subsequently, it turns out this is probably a bug in Mono-Cecil, which PropertyChanged uses:

https://github.com/jbevain/cecil/issues/90

相关问答

更多
  • 看来罪魁祸首是执行IL编织的第三方库(Fody.PropertyChanged)。 如果我没有这个库创建一个新的WPF项目,我可以在Watch窗口中使用Math.Sin() 。 一旦我安装PropertyChanged ,我开始得到“在这种情况下不可用”的错误。 感谢@BACON带领我找到正确的道路。 以下是我在PropertyChanged网站上发布的问题的链接: http://code.google.com/p/propertychanged/issues/detail?id=6&thanks=6&ts ...
  • 你在这里看到的行为是一个错误。 在这种情况下,C#调试器不应显示静态成员。 我向代码库的当前所有者证实了这一点,他将为Visual Studio的下一个版本提交一个错误。 发生这种情况的具体方案是 Just My Code已启用 该类型定义为确定为非用户程序集的内容 引用和对象实例的类型不同(将hash2切换为SHA1Cnf ,问题消失) 请注意,可能会出现其他情况。 这是我在调试/实验中能够缩小范围的行为。 The behavior you are seeing here is a bug. The C# ...
  • Visual Studio将使用项目引用的程序集执行此操作。 使用其名称空间尚未包含但可通过引用的程序集使用的类型。 如果你鼠标悬停它,你会在符号的左下角有一个小弹出窗口。 这将让你自动拉入适当的使用。 我一直使用AssemblyInfo.cs的CLSCompliantAttribute来使用它b / c默认情况下System不可用。 当我发现需要为一些代码添加跟踪时,我也发现自己经常以这种方式引入System.Diagnostics 。 Visual Studio will do that with as ...
  • 我发现了问题。 这是由于延伸“我在哪里”。 我的扩展程序可用于Visual Studio 2010.这不适用于VS 2012.我做了一个hack以便它可以安装在VS 2012上。这导致了这个问题。 我禁用了这个扩展,现在一切正常。 I found the problem. It was due to a extension "Where Am I". Where Am I extension is available for Visual Studio 2010. This is not available ...
  • 请参阅使用DebuggerDisplay属性 如果您通过属性标记了类: [DebuggerDisplay("x = {X} y = {Y}")] public class MyClass { public int X { get; private set; } public int Y { get; private set; } } Watch窗口的Value列中显示的输出如下所示: x = 5 y = 18 See Using DebuggerDisplay Attribute If yo ...
  • 我试图重现您的问题并找到以下内容: 监视窗口似乎使用您在代码中引用的命名空间(通过using )。 如果您不在代码文件中使用linq(和System.Linq命名空间),则监视窗口无法找到扩展名。 如果你有using System.Linq; 并在代码中使用该命名空间中的内容,监视窗口将查找并执行linq扩展。 (如果不使用System.Linq的任何System.Linq ,则会优化引用,因此在运行时不会加载此程序集,并且调试程序无法使用它)。 I tried to reproduce your prob ...
  • 在2012年及以后,答案是Debugger Type Visualizers。 有关2012版本以及详细信息(如果要实现自定义的详细信息), 请参阅Visual Studio团队博客,查看MSDN文档。 In 2012 and later, the answer is Debugger Type Visualizers. See the Visual Studio team blog for the 2012 version and for the nitty gritty details (if you ...
  • 这是由于Fody。 我们停止使用Fody并解决了这个问题。 Super late reply, but as of sometime around the beginning of 2018 Fody no longer causes this problem. It was a Cecil issue and that was fixed with Cecil 0.10.
  • 再试一次,没有连接到进程(而是点击F5),并确保勾选了“启用VIsual Studio托管进程”(右键单击项目 - >属性 - >调试)复选框。 关于为什么有时会出现这种情况的解释,有时候没有,以及为什么我提供的建议可能有所帮助,请参阅JaredPar关于该主题的博客文章。 Like many bugs, it disappeared and I have no idea why. :(
  • 我担心在VS2010观察窗口中没有这种内置的搜索功能(尽管观察窗口有很多很酷的功能 )。 另请参阅此未解决的问题: 在visual studio中的Object watch窗口中搜索文本 然而,有一些商业扩展提供了这种功能。 例如:摘自OzCode VS扩展功能列表: 搜索 调试对象和集合时,通常会查找特定属性或字段,或者属性或字段中保存的值。 不幸的是,这通常涉及大量点击和滚动,或编写自定义调试特定代码。 即使在简单的结构中查找项目也不容易,更不用说在复杂的对象图中这样做了。 通过我们的“搜索”功能,情况 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。