在C使用ARGV? [英] Using argv in C?

查看:172
本文介绍了在C使用ARGV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关的转让,我需要有命令行参数我的C程序。我用的argc / argv的(在C ++)之前没有麻烦,但我不确定如果C风格的字符串正在影响如何工作的。这里是我的主要的启动:

For an assignment, I am required to have command line arguments for my C program. I've used argc/argv before (in C++) without trouble, but I'm unsure if C style strings are affecting how this works. Here is the start of my main:

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

  if(argc>1){
    printf("0 is %s, 1 is %s\n",argv[0],argv[1]);
    if(argv[1]=="-e"){
        // Do some stuff with argv[2]
        system("PAUSE");
    }
    else{
        printf("Error: Incorrect usage - first argument must be -e");
        return 0;
    }
  }

所以我打电话给我的程序作为的Program.exe -e myargstuff,但我收到错误:不正确的使用方法......输出,即使我的printf()告诉我的argv [1] - E。一些帮助,好吗?谢谢!

So I am calling my program as "program.exe -e myargstuff" but I am getting the "Error: Incorrect Usage..." output, even though my printf() tells me that argv[1] is "-e". Some help, please? Thanks!

推荐答案

if(argv[1]=="-e"){

比较指针,不是字符串。使用 STRCMP 函数:

if(strcmp(argv[1],"-e")==0){

这篇关于在C使用ARGV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆