如何使用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)
注意:我具体不是在谈论使用传递数组,vsprintf()因为只有 1 个值。对我来说多次传递相同的值似乎毫无意义。
谢谢
下面是 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) 我正在编写一个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.为什么?
我正在尝试使用-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) 例如,是否可以清除 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)
本质上,我想让一些文本留在同一个地方,并在不清除整个屏幕的情况下重绘更新数据。
我想迭代所有(至少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.
我正在调试一个应用程序来修复我怀疑是由竞争条件引起的分段错误。
我想在代码中添加一些打印语句,但我知道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 上运行。
在 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 颜色和工作字段宽度的干净方法是什么?
如果在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) 我有一个简单的代码来读取浮点数并以六位小数打印它。
我的 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( …