标签: printf

php sprintf 用一个值替换多个占位符

如何使用sprintf()一个值替换字符串中的多个占位符?我知道通常您会为每个占位符传递 1 个变量,但我知道如果您使用格式%1$s而不是%s可以传递单个值,但我似乎无法让它工作。

我实际上将其用作参数化的 sql 查询,但为了方便起见,这里有一个示例

$name   = "Bill";
$string = "hello ?, your name is ? ";
$string = sprintf(str_replace("?","'%1$s'",$string ),$name);
Run Code Online (Sandbox Code Playgroud)

这似乎不起作用。我还希望它适用于单个占位符,例如

$name   = "Bill";
$string = "hello ?";
$string = sprintf(str_replace("?","'%1$s'",$string ),$name);
Run Code Online (Sandbox Code Playgroud)
  1. 是否可以用一个值替换多个占位符,以及 2. 是否可以编写一条语句来处理单个或多个占位符?

注意:我具体不是在谈论使用传递数组,vsprintf()因为只有 1 个值。对我来说多次传递相同的值似乎毫无意义。

谢谢

php printf str-replace

5
推荐指数
1
解决办法
5566
查看次数

取消 pthread 和 printf 行为

下面是 pthread 取消的代码示例:

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

void *my_routine(void *arg) {
  int i;
  for (i = 0; i < 10; i++) {
    printf("%d\n", i);
  }
  return NULL;
}

int main(void) {
  pthread_t thread;
  if (pthread_create(&thread, NULL, my_routine, NULL)) {
    fprintf(stderr, "Cannot create pthread\n");
    return 1;
  }
  usleep(20);
  pthread_cancel(thread);
  pthread_join(thread, NULL);
  //fflush(stdout);
  sleep(1);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我编译:

gcc -pthread -Wall threadtest.c -o threadtest
Run Code Online (Sandbox Code Playgroud)

执行时,有时会在 后 sleep(1)打印一个附加数字。

有时这个数字是重复的:

0
1
2
3
4
4  // printed after sleep(1) …
Run Code Online (Sandbox Code Playgroud)

c printf pthreads cancellation

5
推荐指数
1
解决办法
1204
查看次数

如何将printf语句的结果分配给变量(对于前导零)?

我正在编写一个shell脚本(Mac OS X上的Bash)来重命名一堆图像文件.我希望结果如下:

frame_001
frame_002
frame_003
Run Code Online (Sandbox Code Playgroud)

等等

这是我的代码:

let framenr=$[1 + (y * cols * resolutions) + (x * resolutions) + res]
echo $framenr:
let framename=$(printf 'frame_%03d' $framenr)
echo $framename
Run Code Online (Sandbox Code Playgroud)

$framenr看起来是正确的,但$framename总是成为0.为什么?

bash shell printf

5
推荐指数
1
解决办法
1万
查看次数

如何在g ++ -std = c ++ 11版本4.8.2中使用snprintf()

我正在尝试使用-std = c ++ 11的g ++版本4.8.2中的snprintf编译代码,但没有成功.为什么g ++不能识别snprintf?我可以在使用snprintf和c ++ 11时克服这个问题吗?

我得到以下内容:

make all  Building file: ../src/cppHWtest.cpp Invoking: Cygwin C++
Compiler g++ -std=c++11 -D"hash_map=unordered_map" -O0 -g3 -Wall -c
-fmessage-length=0 -MMD -MP -MF"src/cppHWtest.d" -MT"src/cppHWtest.d" -o "src/cppHWtest.o" "../src/cppHWtest.cpp" cygwin warning:   MS-DOS style path detected: C:\Users\poudyal\workspace\cppHWtest\Debug
Preferred POSIX equivalent is:
/cygdrive/c/Users/poudyal/workspace/cppHWtest/Debug   CYGWIN
environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
    http://cygwin.com/cygwin-ug-net/using.html#using-pathnames ../src/cppHWtest.cpp: In function 'int main()':
../src/cppHWtest.cpp:20:35: error: 'snprintf' was not declared in this
scope …
Run Code Online (Sandbox Code Playgroud)

printf cygwin g++ c++11

5
推荐指数
1
解决办法
6329
查看次数

清除多行

例如,是否可以清除 C 中的多行并保留其他行。

代码:

Displaysenrsordata
  loop 
    printf("This info stays"); <-stay on screen
    printf("This info stays"); <-stay on screen
    printf("This info Refreshes"); <-update redraw
    printf("This info Refreshes"); <-update redraw
    printf("This info Refreshes"); <-update redraw
Run Code Online (Sandbox Code Playgroud)

本质上,我想让一些文本留在同一个地方,并在不清除整个屏幕的情况下重绘更新数据。

c terminal console printf

5
推荐指数
2
解决办法
1352
查看次数

如何迭代unicode字符并使用printf在C屏幕上打印它们?

我想迭代所有(至少16位)unicode字符并用C在屏幕上打印它们.

我知道有关于SO的相关问题,但它们并没有解决printfC中的问题,但这是我想要实现的,如果它毕竟是可能的.我认为应该可能有一个我不知道的技巧.

既然我想使用printf,我想到了这样的事情:

for (int i = 0x0000; i <= 0xffff; i++) {

    //then somehow increment the string
    char str[] = "\u25A1\n";
    printf("%s", str);

    char str[] = "\u25A2\n";
    printf("%s", str);

    char str[] = "\u25A3\n";
    printf("%s", str);

    ...

}
Run Code Online (Sandbox Code Playgroud)

但是这里增加unicode代码点有点问题\u25A1.我知道它本身不可能,因为有些字符\u0000不可打印,编译器说不.但除此之外,我怎么能从十六进制0000增加到ffff并打印字符printf.

c unicode printf

5
推荐指数
2
解决办法
4619
查看次数

调试竞争条件时打印信息的最佳方式

我正在调试一个应用程序来修复我怀疑是由竞争条件引起的分段错误。

我想在代码中添加一些打印语句,但我知道printf不建议添加对的调用,因为这可能会改变线程的行为并在某些情况下隐藏错误。

查看其他选项,我已经看到使用gdb可以使用断点打印某些内容,然后自动继续执行:

break foo
commands
silent
printf "Called foo: x is %d\n",x
cont
end
Run Code Online (Sandbox Code Playgroud)

这比printf在我的代码中放入一个更好吗?

我知道gdb也有一个跟踪点,但它们只适用于 gdbserver,这是我目前更愿意避免的额外复杂程度。

附加信息:该应用程序是用 C 编写的,可在 Linux 上运行。

c debugging printf gdb race-condition

5
推荐指数
1
解决办法
1794
查看次数

Python在字符串格式宽度字段中计算零长度控制字符?

在 Python 3.4.3 中,我尝试使用 string.format() 运算符对某些字段进行宽度对齐,它似乎将零长度控制字符与宽度总数进行了计数。示例代码:

ANSI_RED = "\033[31m"
ANSI_DEFAULT="\033[39m\033[49m"

string1 = "12"
string2 = ANSI_RED+"12"+ANSI_DEFAULT

print("foo{:4s}bar".format(string1))
print("foo{:4s}bar".format(string2))
Run Code Online (Sandbox Code Playgroud)

这将输出:

foo12  bar
foo12bar
Run Code Online (Sandbox Code Playgroud)

(第二个输出的“12”为红色,但我无法在 SO 中重现它)

在第二种情况下,我丢失了字段宽度,我假设是因为 Python 看到字符串中的字符总数大于宽度,尽管大多数字符在符合 ANSI 的终端上导致零长度。

拥有 ANSI 颜色和工作字段宽度的干净方法是什么?

python terminal printf

5
推荐指数
1
解决办法
860
查看次数

printf 格式字符串中的变量插值与替换

如果在printf(1)格式字符串中放入变量,ShellCheck会发出警告。为什么?

是:

printf "$file does not exist\n"
Run Code Online (Sandbox Code Playgroud)

在某些方面不如:

printf "%s does not exist\n" "$file"
Run Code Online (Sandbox Code Playgroud)

shell printf shellcheck

5
推荐指数
1
解决办法
1146
查看次数

如何在 Haskell 中像在 C 中一样使用 printf 来舍入浮点数?

我有一个简单的代码来读取浮点数并以六位小数打印它。

我的 C 代码可以工作,我想制作一个与 Haskell 语言相同的代码,而不需要安装任何额外的库。

我的解决方案在 C 和 Haskell 中非常简单:

C:

#include <stdio.h>
int main() {
    float n = 123.141568;
    printf("%.6f\n", n); // 123.141571
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

哈斯克尔:

import Text.Printf (printf)
main = do
  let n = 123.141568 :: Float
  printf "%.6f\n" n -- 123.141570
Run Code Online (Sandbox Code Playgroud)

这些代码对于大多数数字都可以正常工作,但有一些不同的结果。例如,当n = 123.141568C 程序输出123.141571而 Haskell 程序输出123.141570. 如果我在 Haskell 中从 改为 ,Float结果Double是123.141568。

我的目标是让我的 Haskell 代码等于 C。我意识到即使在两种语言中使用 printf,舍入方法也是不同的,但我不知道这是如何发生的。

Float我尝试制作自定义舍入函数,但结果与(在上面的示例中123.141570)和Double( …

floating-point precision printf haskell number-formatting

5
推荐指数
0
解决办法
131
查看次数