将 printf 与非空终止字符串一起使用

发布于 2024-09-24 18:19:21 字数 115 浏览 4 评论 0原文

假设您有一个非 null 终止的字符串,并且您知道它的确切大小,那么如何在 C 中使用 printf 打印该字符串呢?我记得有这样的方法,但现在找不到了......

Suppose you have a string which is NOT null terminated and you know its exact size, so how can you print that string with printf in C? I recall such a method but I can not find out now...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

久而酒知 2024-10-01 18:19:21

printf 有一种可能性,它是这样的:

printf("%.*s", stringLength, pointerToString);

不需要复制任何东西,不需要修改原始字符串或缓冲区。

There is a possibility with printf, it goes like this:

printf("%.*s", stringLength, pointerToString);

No need to copy anything, no need to modify the original string or buffer.

谁对谁错谁最难过 2024-10-01 18:19:21

以下是 %.*s 的工作原理及其指定位置的说明。

printf 模板字符串中的转换规范具有一般形式:

% [ param-no $] 标志宽度 [ .精度] 类型转换

% [ param-no $] 标记宽度。 * [param-no $] 类型转换

第二种形式用于从参数列表中获取精度:

您还可以指定精度“*”。这意味着参数列表中的下一个参数(在要打印的实际值之前)用作精度。该值必须是 int,如果为负数则被忽略。

— 输出转换语法< glibc 手册中的 /a>

对于 %s 字符串格式化,精度有特殊含义:

可以指定一个精度来表示要写入的最大字符数;否则,字符串中直到但不包括终止空字符的字符将被写入输出流。

— 其他输出转换< glibc 手册中的 /a>

其他有用的变体:

  • "%*.*s", maxlen, maxlen, val 将右对齐,在前面插入空格;
  • "%-*.*s​​", maxlen, maxlen, val 将左对齐。

Here is an explanation of how %.*s works, and where it's specified.

The conversion specifications in a printf template string have the general form:

% [ param-no $] flags width [ . precision ] type conversion

or

% [ param-no $] flags width . * [ param-no $] type conversion

The second form is for getting the precision from the argument list:

You can also specify a precision of ‘*’. This means that the next argument in the argument list (before the actual value to be printed) is used as the precision. The value must be an int, and is ignored if it is negative.

— Output conversion syntax in the glibc manual

For %s string formatting, precision has a special meaning:

A precision can be specified to indicate the maximum number of characters to write; otherwise characters in the string up to but not including the terminating null character are written to the output stream.

— Other output conversions in the glibc manual

Other useful variants:

  • "%*.*s", maxlen, maxlen, val will right-justify, inserting spaces before;
  • "%-*.*s", maxlen, maxlen, val will left-justify.
听不够的曲调 2024-10-01 18:19:21

您可以使用 fwrite() 来输出!

fwrite(your_string, sizeof(char), number_of_chars, stdout);

这样,您将把第一个字符(在 number_of_chars 变量中定义的数字)输出到一个文件,在本例中输出到 stdout(标准输出,您的屏幕)!

You can use an fwrite() to stdout!

fwrite(your_string, sizeof(char), number_of_chars, stdout);

This way you will output the first chars (number defined in number_of_chars variable ) to a file, in this case to stdout (the standard output, your screen)!

残花月 2024-10-01 18:19:21

printf("%.*s", length, string) 将不起作用。

这意味着打印最多长度字节或空字节,以先到者为准。如果您的非空终止字符数组在长度之前包含空字节,则 printf 将在这些字节上停止,并且不会继续。

printf("%.*s", length, string) will NOT work.

This means to print UP TO length bytes OR a null byte, whichever comes first. If your non-null-terminated array-of-char contains null bytes BEFORE the length, printf will stop on those, and not continue.

山人契 2024-10-01 18:19:21
printf("%.5s", pointerToNonNullTerminatedString);

字符串长度将为 5。

printf("%.5s", pointerToNonNullTerminatedString);

The string length will be 5.

苏别ゝ 2024-10-01 18:19:21
#include<string.h> 
int main()
{
/*suppose a string str which is not null terminated and n is its length*/
 int i;
 for(i=0;i<n;i++)
 {
 printf("%c",str[i]);
 }
 return 0;
}

我编辑了代码,这是另一种方式:

#include<stdio.h>
int main()
{
printf ("%.5s","fahaduddin");/*if 5 is the number of bytes to be printed and fahaduddin is the string.*/

return 0;

}
#include<string.h> 
int main()
{
/*suppose a string str which is not null terminated and n is its length*/
 int i;
 for(i=0;i<n;i++)
 {
 printf("%c",str[i]);
 }
 return 0;
}

I edited the code,heres another way:

#include<stdio.h>
int main()
{
printf ("%.5s","fahaduddin");/*if 5 is the number of bytes to be printed and fahaduddin is the string.*/

return 0;

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