[英]Remove the space from infront of my C String
I have some code that outputs like this: 我有一些输出像这样的代码:
void exec_prompt(char * usr_name, char * host_name)
{
printf(" %s::%s\n", usr_name, host_name);
return;
}
But the print out doesnt look as expected: 但打印输出看起来并不像预期的那样:
geisst::ALPHA-DT2
There is that space at the front of the string. 在弦的前面有那个空间。
The usr_name variable is passed from the main function and is returned from the getenv() function. usr_name变量从main函数传递,并从getenv()函数返回。 The host_name variable is passed from the main function with the use of the following function:
使用以下函数从main函数传递host_name变量:
char * returnHost()
{
char hostname[1024];
hostname[1023] = '\0';
gethostname(hostname, 1023);
return hostname;
}
Maybe the getenv() function adds a space? 也许getenv()函数增加了一个空格?
Any help or advice is appreciated and please be nice :P 任何帮助或建议表示赞赏,请保持友善:P
GeissT 盖斯特
The reason is that your format has a space: " %s::%s\\n" 原因是你的格式有一个空格:“%s ::%s \\ n”
Just change it to: 只需将其更改为:
printf("%s::%s\n", usr_name, host_name);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.