简体   繁体   English

做 ';' 以某种方式终止您作为命令行参数传递的字符串?

[英]Does ';' somehow terminates a string that you pass as a command line argument?

I'm making a program that gets a text input from the command line.我正在制作一个从命令行获取文本输入的程序。

Basically it takes every command line argument as a string, and every string represents a word within the "text", and I want to print the "text" that gets passed from the command line (without the name of my output) to the screen.基本上它将每个命令行参数作为一个字符串,每个字符串代表“文本”中的一个单词,我想打印从命令行传递的“文本”(没有我的输出名称)到屏幕.

But if one of my command line arguments is a word-string such as 'Hello;'但是,如果我的命令行 arguments 之一是字符串,例如“Hello;” or 'And)', with a ';'或“和)”,带有“;” or an ')' at the end of it -as you can see- then it doesn't print the ';'或结尾的“)”-如您所见-然后它不会打印“;” or the ')' etc.或“)”等。

And the way I see it is that, it doesn't even recognize these symbols as a part of the string that gets passed in.我看到它的方式是,它甚至不将这些符号识别为传入的字符串的一部分。

I even tried to print the ';'我什至尝试打印';' symbol in a simple 'Hello;'简单的“你好”中的符号; example just to see what is it going to do (Specifically I wrote some code to print only the last character of each string argument).示例只是为了看看它会做什么(特别是我写了一些代码来只打印每个字符串参数的最后一个字符)。

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

int main(int argc , char *argv[]){
int i;
    for(i=1 ; i < argc ; i++){
        char p = *(argv[i] + strlen(argv[i]) - 1);
         printf("%c " , p);
    }
printf("\n");
return 0;
}

When i run the code from command line like this:当我像这样从命令行运行代码时:

./a.out Hello; ./a.out 你好;
It prints它打印
o
Instead of代替
; ;

I've tried to print ';我试图打印'; ' by itself, it just prints nothing and i get an error for the very next string after these '; ' 就其本身而言,它什么也不打印,并且在这些之后的下一个字符串出现错误 '; ', ' ) ' symbols such as error command not found. ', ' ) ' 符号,例如找不到错误命令。

If anyone knows why this is happening, or how can I make this program print even ';'如果有人知道为什么会这样,或者我怎样才能让这个程序打印甚至';' symbols etc. please help me, I'm new to C and to programming in gereral.符号等请帮助我,我是 C 和普通编程的新手。

The ; ; character is interpreted specially by the shell.字符由 shell 专门解释。 It is used to separate multiple commands on the same line.它用于分隔同一行上的多个命令。

So you're either need to quote the string:所以你要么需要引用字符串:

./a.out "Hello;"

Or escape the ;或逃脱; :

./a.out Hello\;

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

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