我有一个字符串const char[15],我想像这样打印:
标签一:字符[0,13]
标签二:字符[13,15]
如何只打印字符串的某些部分?
我正在尝试为我们的内部开发服务器自动设置站点.
目前,这包括创建系统用户,mysql用户,数据库和apache配置.我知道如何在一个bash文件中完成所有操作,但我想问一下是否有办法更干净地生成apache配置.
基本上我想要做的是生成基于模板的conf文件,类似于使用printf.我当然可以使用printf,但我认为可能有一种更干净的方式,使用sed或awk.
我不只是想使用printf的原因是因为apache配置大约有20行,并且会占用大部分bash脚本,并且使其更难阅读.
任何帮助表示赞赏.
我知道这两个函数都可以用来输出到控制台.
我读了这个问题,但没有人告诉我在输出到控制台时更喜欢使用哪个.那么哪个功能更好,有什么重大差异吗?
新手在这里,但我最终在我的智慧.我已经完成了我的程序,但是在打印时只需要格式化方面的帮助.
if(count == 3)
System.out.printf ("%-15s %15s %15s %15s %15s %n", n, " is compatible with
",dates[k],dates[k+1],dates[k+2]);
Run Code Online (Sandbox Code Playgroud)
我的输出是
Stacey Francis is compatible with Owen Farrell Jack Clifford Joshua Watkins
Run Code Online (Sandbox Code Playgroud)
我希望我的输出是(不重复斯泰西弗朗西斯名称或"兼容":
Stacey Francis is compatible with Owen Farrell
Jack Clifford
Joshua Watkins
Run Code Online (Sandbox Code Playgroud)
只是想知道如何解决这个问题?
谢谢,
科斯蒂
当我使用sprintf时,结果显示如下:
sprintf('number=%d %d %d',a,b,c)
sprintf('or %d',h)
ans =
number= 5 4 2
ans =
or 2
Run Code Online (Sandbox Code Playgroud)
如何在不ans =妨碍结果的情况下显示结果?
snprintf(3)的Linux手册页给出了以下示例:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
char *
make_message(const char *fmt, ...)
{
int n;
int size = 100; /* Guess we need no more than 100 bytes */
char *p, *np;
va_list ap;
if ((p = malloc(size)) == NULL)
return NULL;
while (1) {
/* Try to print in the allocated space */
va_start(ap, fmt);
n = vsnprintf(p, size, fmt, ap);
va_end(ap);
/* Check error code */
if (n < 0)
return NULL;
/* If that worked, return …Run Code Online (Sandbox Code Playgroud) 在我编写单元测试时,我偶然发现了一些奇怪的行为glibc,关于"%p"和NULL指针.
如果我有一条这样的线printf("NULL pointer is %p\n", NULL);,那么我看到NULL pointer is (nil)印在屏幕上,正如我所料.
如果我改为使用宽字符版本:wprintf(L"NULL pointer is %p\n", NULL);,则打印出来NULL pointer is (,并在左括号处停止.如果我打印非NULL指针,它会打印该指针,包括普通和宽字符版本.这是一个已知的错误glibc,或者我只是遗漏了什么?
注意:我意识到C标准说指针以%p实现定义的方式转换; 只是打印(一个NULL指针似乎不寻常.
我在C语言中以字符串定义的十六进制数字:
char chars[] = "\xfb\x54\x9c\xb2\x10\xef\x89\x51\x2f\x0b\xea\xbb\x1d\xaf\xad\xf8";
Run Code Online (Sandbox Code Playgroud)
然后我想将这些值与另一个值进行比较.它不起作用,如果我打印的值如下:
printf("%02x\n", chars[0]);
Run Code Online (Sandbox Code Playgroud)
它写道fffffffb.为什么这样以及如何fb准确地获得价值?
我想将__int64变量转换为CString.代码就是这样
__int64 i64TotalGB;
CString totalSpace;
i64TotalGB = 150;
printf("disk space: %I64d GB\n", i64TotalGB);
totalSpace.Format(_T("%I64d", i64TotalGB));
printf("totalSpace contains: %s", totalSpace);
Run Code Online (Sandbox Code Playgroud)
第一个printf打印
"disk space: 150GB"
Run Code Online (Sandbox Code Playgroud)
它是正确的,但第二个printf打印随机高数字,如
"totalSpace contains: 298070026817519929"
我也尝试使用INT64变量而不是__int64变量,但结果是一样的.这可能是什么原因?
我写了一个小代码,用特定格式打印当前日期.要返回日期的每个数据(星期几,月中的某天,月份的名称等),我使用const char*类型的函数将数据作为字符串传递给充当缓冲区的变量然后将缓冲区传递给main函数.
以下代码以非常简单的方式表示我的程序的操作:
#include <stdio.h>
#include <string.h>
char
buff[16];
const char
*printStr1 (char *str1)
{
strncpy(buff, str1, sizeof(str1));
return buff;
};
const char
*printStr2 (char *str2)
{
strncpy(buff, str2, sizeof(str2));
return buff;
};
int
main()
{
printf("%s%s", printStr1("foo"), printStr2("bar"));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:当将每个数据传递给缓冲区并将其返回到main函数时,最后两个数据是相同的(最后一个数据不打印,而是打印已经存在于缓冲区中的数据,即倒数第二个数据).
这只发生在我使用单个printf()函数来显示所有数据时,如上例所示.如果我不使用缓冲区返回数据(我直接返回传递给函数的参数)或对每个数据使用printf(),一切都正确执行.
PS:我试图用memset()这样的函数来解决这个问题来清理缓冲区,但它不起作用,我唯一能做的就是为每个数据使用不同的缓冲区(对于每个函数).
我希望我能很好地表达我的问题.