简体   繁体   English

.exe程序的C ++命令行参数

[英]C++ command line argument for .exe program

I have program.exe that is being created on my Windows machine. 我的Windows机器上正在创建program.exe For some reason I am not able to pass in command line arguments properly. 由于某种原因,我无法正确传递命令行参数。

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

  ///////testing
  cout << "\n\n(int)ARGV[1]: " << (int)argv[1] << "\n\n";

  return 0;
} 

In terminal I run: 在终端中,我运行:

program.exe 4 

I see (int)ARGV[1]: 15333464 printed to my console. 我看到(int)ARGV[1]: 15333464打印到我的控制台。

Any idea why this is or how I can modify the code? 知道为什么会这样或如何修改代码吗? I should get the number 4 printed out. 我应该把数字4打印出来。 Thanks. 谢谢。

When you cast from char* to int you get the pointer value bitpattern interpreted as an integer. char*int您将指针值bitpattern解释为整数。

A good way to instead interpret the character string pointed to, as a specification of an integer, is to use std::stoi from the <string> header. 代替将指向的字符串解释为整数的一种好方法,是使用<string>头中的std::stoi

Thus: 从而:

#include <iostream>
#include <stdlib.h>    // EXIT_FAILURE
#include <string>      // std::stoi
using namespace std;

int main(int argc, char *argv[])
{
    if( argc != 2 )
    {
        return EXIT_FAILURE;
    }
    cout << "ARGV[1]: " << stoi( argv[1] ) << "\n";
} 

For the case where the argument isn't a valid specification of an integer, stoi will throw an exception, and in the code above that will cause the program to terminate with some message displayed – a crash. 对于参数不是整数的有效stoi的情况, stoi将引发异常,并且在上面的代码中,这将导致程序终止并显示一些消息,即崩溃。 That's generally preferable to producing incorrect results. 通常比产生不正确的结果更好。 But if you want to handle it, read up on try and catch in your C++ textbook. 但是,如果你要处理它,在阅读了trycatch在C ++的教科书。


If you want filenames as command line arguments, and more generally filesystem paths, then do note that the Windows convention for encoding of char based strings is Windows ANSI, which has a very limited set of characters. 如果要使用文件名作为命令行参数,更广泛地说是文件系统路径,则请注意,用于对基于char的字符串进行编码的Windows约定是Windows ANSI,它的字符集非常有限。 Some filenames and paths on my Norwegian computer can't be represented in your Windows ANSI (yes, Windows ANSI is locale-specific). 挪威语计算机上的某些文件名和路径无法在Windows ANSI中表示(是的,Windows ANSI是特定于语言环境的)。 So for this the C and C++ main argument mechanism, is ungood. 因此,为此C和C ++ main参数机制是不好的。

The Windows API provides a pair of wchar_t based functions that can be used as a (working) alternative, namely GetCommandLine (retrieves the raw UTF-16-encoded command line) and CommandLineToArgvW (standard parsing to produce individual arguments). Windows API提供了一对基于wchar_t的函数,它们可以用作(有效的)替代方法,即GetCommandLine (获取原始的UTF-16编码的命令行)和CommandLineToArgvW (用于产生各个参数的标准解析)。

Some Windows compilers also provide an alternative to standard main called wmain , where argv is declared as wchar_t* argv[] . 一些Windows编译器还提供了标准main wmain的替代方法,其中argv被声明为wchar_t* argv[] These compilers include Visual C++ and the MinGW64 variant of g++. 这些编译器包括Visual C ++和g ++的MinGW64变体。

Here argv[1] is a character pointer ( char* ) so you cant convert a character pointer to an integer by type casting. 这里argv[1]是字符指针( char* ),因此您不能通过类型转换将字符指针转换为整数。 So to convert the character pointer to an integer use the atoi() function, which is available in the c standard library (include the cstdlib library). 因此,要将字符指针转换为整数,请使用atoi()函数,该函数在c标准库(包括cstdlib库)中可用。

#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;
using std::atoi;
int main(int argc, char *argv[])
{
    cout << atoi(argv[1]) << " " << endl;
    return 0;
}

Note that atoi() returns zero if the argument passed to it cannot be converted to an integer. 请注意,如果传递给它的参数不能转换为整数,则atoi()返回零。 So you could check the returned value and if it is zero then print the relevant message. 因此,您可以检查返回的值,如果该值为零,则输出相关消息。

Thus: 从而:

#include <iostream>
#include <cstdlib>
#include <cstring>
using std::atoi;
using std::cout;
using std::endl;
using std::strcmp;

int main(int argc, char *argv[])
{
    if(argc == 2)
    {
        if(atoi(argv[1]) == 0 && strcmp(argv[1], "0") != 0)
            cout << "The argument supplied is not an integer" << endl;
        else
            cout << atoi(argv[1]) << " " << endl;
    }
    else if( argc > 2)
        cout << "Too many arguements" << endl;
    else
        cout << "Insufficient arguements" << endl;
    return 0;
}

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

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