Accessing command line parameters/arguments

When you start a program from the command line, it is sometimes necessary to use parameters along with the program name. For example:


copy source.txt target.txt


This is known as using command line arguments or parameters. To access them within your program, you need to declare the main() function correctly:


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


Read here for a more detailed explanation of the main() definition.


If the value of argc is greater than 0, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which will be the command line arguments. argv[0] will contain the program name, argv[1] the first command line arg, argv[1] the second and so on. Finally, argv[argc] is guaranteed to the a NULL pointer, which can be useful when looping through the array.


Here are some snippets of code showing ways to access the command line arguments.


#include <stdio.h> 
#include <string.h> 


int main(int argc, char *argv[])
{
  /*
   * Look for -a or -d in command line
   */
  int i;
  
  for (i = 1; i < argc; i++)
  {
    if (argv[i][0] == '-')
      if (argv[i][1] == 'a')
        puts ("Found -a");
      else if (argv[i][1] == 'd')
        puts ("Found -d");
      else printf ("Unknown switch %s\n", argv[i]);
    else if (strcmp(argv[i], "name") == 0)
      puts ("Found name");
  }
  
  return(0);
}


/*
 * Program output when run as
  myprog.exe -a -b -d name
  
  Found -a
  Unknown switch -b
  Found -d
  Found name
*/




And some more:


#include <stdio.h> 


int main(int argc, char *argv[])
{
  if (argc > 0)
    printf ("Program name is >%s<\n", argv[0]);
    
  return 0;  
}




#include <stdio.h> 


int main(int argc, char *argv[])
{
    int i;
    for (i = 1; i < argc; i++)
        puts(argv[i]);
    return 0;
}




#include <stdio.h> 


int main(int argc, char *argv[])
{
while (--argc)
printf ("%s ", *++argv);
return 0;
}






This next program prints the command line details in a manner which should help you understand how the arrays are accessed in memory.


#include <stdio.h> 


int main(int argc, char *argv[])
{
  int i, j;
  for (i = 0; i < argc; i++)
  {
    printf ("argv[%d] is %s\n", i, argv[i]);
    for (j = 0; argv[i][j]; j++)
      printf ("argv[%d][%d] is %c\n", i, j, argv[i][j]);
  }
  return(0);
}


/*
 * When invoked with:
 E:\>a.exe -parm1 -a
 * The output is:
  argv[0] is E:\a.exe
  argv[0][0] is E
  argv[0][1] is :
  argv[0][2] is \
  argv[0][3] is a
  argv[0][4] is .
  argv[0][5] is e
  argv[0][6] is x
  argv[0][7] is e
  argv[1] is -parm1
  argv[1][0] is -
  argv[1][1] is p
  argv[1][2] is a
  argv[1][3] is r
  argv[1][4] is m
  argv[1][5] is 1
  argv[2] is -a
  argv[2][0] is -
  argv[2][1] is a
 *

 */


From: www.Cprogramming.com


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 根据提供的信息,这个问题似乎是和Pentaho中的插件存储库相关的访问问题。 "Pentaho" 是一种用于数据集成和业务智能(BI)的开源工具。而 "/@pentaho/di-plugin-repositories@8.3.0.0-371/index.h" 是指特定版本的一个插件存储库的索引文件。 如果出现 "spoon problem accessing /@pentaho/di-plugin-repositories@8.3.0.0-371/index.h" 的错误信息,可能是由于以下几个原因之一: 1. 插件存储库不可访问:这可能是由于网络连接问题或存储库服务器错误引起的。你可以尝试检查网络连接是否正常,或者联系Pentaho支持团队获取有关存储库服务器状态的更多信息。 2. 插件存储库路径错误:如果路径指定不正确,或者文件路径被更改或删除,就可能无法访问索引文件。你可以验证索引文件的路径是否正确,并确保文件存在。 3. 插件存储库版本不匹配:错误信息中提到了特定的插件存储库版本号(8.3.0.0-371),可能是由于Pentaho版本与该插件存储库版本不兼容导致的。你可以尝试与Pentaho版本对应的插件存储库版本,或者升级Pentaho到与该插件存储库版本兼容的版本。 综上所述,通过验证网络连接、检查存储库路径和确保兼容的Pentaho版本,你可以尝试解决这个问题。如果问题仍然存在,建议联系相关技术支持人员获取进一步的帮助。 ### 回答2: spoon问题访问 /@pentaho/di-plugin-repositories@8.3.0.0-371/index.h的原因可能有以下几种: 1. 网络问题:如果你的电脑无法访问这个地址,可能是因为网络连接出现了问题。你可以尝试重新连接到网络或者使用其他网络环境进行尝试。 2. 路径错误:可能是路径设置错误导致无法访问。你可以检查一下路径是否正确,并确认文件或文件夹是否存在。 3. 权限问题:有时候你可能没有访问该文件或文件夹的权限,导致无法访问。你可以尝试使用管理员权限或者联系系统管理员解决权限问题。 4. 插件版本不匹配:如果你的Spoon版本与这个插件版本不兼容,可能会引发访问问题。你可以尝试更新Spoon或找到与你当前版本兼容的插件。 如果以上解决方法都无法解决问题,建议你查看详细的错误信息或者寻求专业的技术支持来解决这个问题。希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值