我可以将整个字符串作为一个命令行参数传递给 C 中的 argv 吗? [英] Can I pass a whole string as one command line argument into argv in C?

查看:18
本文介绍了我可以将整个字符串作为一个命令行参数传递给 C 中的 argv 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的程序:

Say I have a program like this:

int main(int argc, char **argv){
    printf("The name of the program is %s, and the string passed is:\n %s \n\n", argv[0], argv[1]);
    return 0;
}

我意识到应该进行错误检查等等,但我真正想知道的是如何将整个字符串作为第一个参数传递,或者是否有可能.谢谢!

I realize there should be error checking and whatnot, but what I'd really like to know is how to pass a whole string as the first argument or if it's even possible. Thanks!

推荐答案

只需使用引号

./file_name "This is the long string argument that comes in argv[1]"
     |
     |
     +--- argv[0]

<小时>

编辑:

aruisdante:单引号和双引号的处理取决于shell,双引号可能会做意想不到的事情

aruisdante: single and double quotes are treated depending on the shell, double quotes might do unexpected things

PS:我的答案来自 Windows 平台,其中使用单引号,只是跳过空格后的所有内容

这篇关于我可以将整个字符串作为一个命令行参数传递给 C 中的 argv 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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