删除我的 C 字符串前面的空格
我有一些输出如下的代码:
void exec_prompt(char * usr_name, char * host_name)
{
printf(" %s::%s\n", usr_name, host_name);
return;
}
但打印输出看起来并不像预期的那样:
geisst::ALPHA-DT2
字符串前面有空格。
usr_name 变量从 main 函数传递并从 getenv() 函数返回。 host_name 变量是使用以下函数从主函数传递的:
char * returnHost()
{
char hostname[1024];
hostname[1023] = '\0';
gethostname(hostname, 1023);
return hostname;
}
也许 getenv() 函数添加了一个空格?
感谢任何帮助或建议,请友善:P
GeissT
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. The host_name variable is passed from the main function with the use of the following function:
char * returnHost()
{
char hostname[1024];
hostname[1023] = '\0';
gethostname(hostname, 1023);
return hostname;
}
Maybe the getenv() function adds a space?
Any help or advice is appreciated and please be nice :P
GeissT
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是你的格式有空格:“%s::%s\n”
只需将其更改为:
The reason is that your format has a space: " %s::%s\n"
Just change it to: