简体   繁体   English

在C ++中传递命令行参数

[英]Passing Command-line Arguments in C++

I've been trying to learn how to extend Python 3 with C++, and I was recommended using Boost. 我一直在尝试学习如何使用C ++扩展Python 3,建议使用Boost。 I believe I've followed the procedure of setting up Boost::Python correctly so far, and I have the following code from here (saved as example.cpp) which builds successfully: 我相信到目前为止,我已经按照正确设置了Boost :: Python的过程进行操作,并且我从这里获得了以下代码(保存为example.cpp),该代码可以成功构建:

#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
{
    std::string line;
    boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );

    while (std::cin)
    {
        std::getline(std::cin, line);
        boost::smatch matches;
        if (boost::regex_match(line, matches, pat))
            std::cout << matches[2] << std::endl;
    }
}

The instructions suggested testing this worked by creating a text file called jayne.txt and saving data inside it, and then executing the program from a command prompt and passing in the path to the file as an argument. 指令建议通过创建一个名为jayne.txt的文本文件并将其保存在其中,然后从命令提示符处执行程序并将该文件的路径作为参数传递来测试此工作。 I don't know C++ very well at all and I've been having difficulties with this. 我一点都不了解C ++,对此我一直遇到困难。

I've tried opening the command prompt, and running "path/to/example.cpp" < "path/to/jayne.txt" and "path/to/example.cpp" "path/to/jayne.txt", both from a regular command prompt and from Visual Studio Command Prompt. 我尝试打开命令提示符,并运行“ path / to / example.cpp” <“ path / to / jayne.txt”和“ path / to / example.cpp”“ path / to / jayne.txt”,从常规命令提示符和Visual Studio命令提示符中都可以。 I should be getting output somewhere, but all that happens is that it opens the program in Visual Studio if it's not already open. 我应该在某处获取输出,但是所有发生的事情是,如果尚未打开该程序,它将在Visual Studio中打开该程序。

I'm working with MVSC++ 2010 in Windows 7. 我正在Windows 7中使用MVSC ++ 2010。

C++ is a compiled language - thus, in order to run a program, you need to run the compiled version ( .exe ) rather than the source code ( .cpp ). C ++是一种编译语言-因此,要运行程序,您需要运行编译版本( .exe ),而不是源代码( .cpp )。

When you type "path/to/example.cpp" at the command line, it's opening the source code. 当您在命令行中键入"path/to/example.cpp"时,它将打开源代码。 Find the actual executable from building your code (generally it's located in a build or Debug folder for MSVC) and run that on the command line and the < operator should work fine. 从构建代码中查找实际的可执行文件(通常位于MSVC的buildDebug文件夹中),然后在命令行上运行该可执行文件,并且<运算符应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM