删除我的 C 字符串前面的空格

发布于 2024-12-24 16:57:52 字数 553 浏览 1 评论 0原文

我有一些输出如下的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山人契 2024-12-31 16:57:52

原因是你的格式有空格:“%s::%s\n”

只需将其更改为:

printf("%s::%s\n", usr_name, host_name);

The reason is that your format has a space: " %s::%s\n"

Just change it to:

printf("%s::%s\n", usr_name, host_name);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文