如何使用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 个值。对我来说多次传递相同的值似乎毫无意义。
谢谢
在记事本++中如果我有代码
printf("Hello, World\n");
Run Code Online (Sandbox Code Playgroud)
整个字符串 '"Hello, World\n"' 是一种颜色,但在 vim 等编辑器中 '\n' 是不同的颜色,表示它是一个特殊字符。在记事本++中是否有这种行为?vim 的一个特别好的功能是突出显示一些陌生的 printf 语法,例如 '% 5.3s' 或 '%%'
下面是 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) 不确定我是否在这里遗漏了一些东西,当我尝试打印网址时它会抛出错误。
错误字符串与 Printf.TextWriteFormat 不兼容
type Credentials = {clientId: string; clientSecret: string}
let credentials = {clientId = ""; clientSecret = ""; deviceId = ""; deviceToken = ""}
let host = "https://example.com?grant_type=token";
let url = String.Format("%s?&client_id=%s&client_secret=%s", host, credentials.clientId, credentials.clientSecret)
printf url
let request = WebRequest.Create(url)
Run Code Online (Sandbox Code Playgroud)
编辑请求标头接受不起作用
let request = WebRequest.Create(url)
request.Method <- "POST"
request.ContentType <- "application/x-www-form-urlencoded"
//request.Headers.Set "Accept" "application/json;charset=UTF-8"
request.ContentLength <- (int64)data.Length
request.Accept <- "application/json;charset=UTF-8" // doesnt' work either
Run Code Online (Sandbox Code Playgroud) 我应该先说一下,我对 VS 比较陌生,但对 C 来说并不陌生。
我遇到的问题是打印时标准输出上没有显示任何内容。printf/_s 和 fprintf/_s(stdout, ...) 都不会产生任何输出。有趣的是, fprinf(file, ...) 实际上确实会生成给定文件的输出。这是否有可能与 printf 弃用有关(我已经尝试过 preproc._CRT_SECURE_NO_DEPRECATE)?
下面是我的完整程序:
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
FILE * pFile = fopen("outputTest.txt", "w");
fprintf(pFile, "At top of main1.\n"); //works
printf("At top of main2.\n"); //doesn't work
printf_s("At top of main3.\n"); //doesn't work
fprintf_s(stdout, "At top of main4.\n"); //doesn't work
fflush(stdout);
fclose(pFile);
return FALSE;
}
Run Code Online (Sandbox Code Playgroud)
我使用的是 Visual Studio 2017,该程序是 Win32(应用程序?)。我还排除了链接器->系统->子系统是问题的可能性。
任何想法表示赞赏。 …
我必须打印 0-19 之间的 10 的幂。问题是当我想显示第一个幂(应该是 1 )时(10^0),我只是无法强制不printf重复 0 。我只能printf在我的程序中使用一个和一个{ }块(这是主函数)。
#include <stdio.h>
int main(void) {
int power = 19;
for (int i = 0; i <= power; i++)
printf("1%0*d\n",i, 0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的输出:
10 // this should be 1, but printf still puts 0 here
10
100
1000
10000
100000
1000000
10000000
100000000
1000000000
10000000000
100000000000
1000000000000
10000000000000
100000000000000
1000000000000000
10000000000000000
100000000000000000
1000000000000000000
10000000000000000000
Run Code Online (Sandbox Code Playgroud) 令人尊敬的snprintf()功能...
int snprintf( char *restrict buffer, size_t bufsz, const char *restrict format, ... );
Run Code Online (Sandbox Code Playgroud)
缓冲区大小为size_t,但返回类型仅为 an ,这有什么意义int?
如果snprintf()应该能够将多个INT_MAX字符打印到缓冲区中,那么它肯定必须返回一个ssize_t或一个size_t以(size_t) - 1指示错误,对吧?
如果它不能打印多个INT_MAX字符,为什么是bufszasize_t而不是 anunsigned或 an int?或者 - 至少官方限制其值不大于INT_MAX?
我满足了在控制台中对齐输出文本的要求。输出文本存储为std::wstring并编码为UTF-8. 使此任务变得棘手的是,输出文本同时包含 ASCII 字符和日语字符,例如\xe3\x83\x8a5\xe5\x9b\x9e1\xe3\x82\xb7\xe3\x83\xa7\xe3\x83\xb34a\xe3\x82\xb9F。由于ASCII字符和日文字符的宽度不同,使用setw()或直接无法对齐。\n例如,wprintf(L"%-10s")
#include <iostream>\n#include <iomanip>\nusing namespace std;\nint main(){\n std::locale::global(std::locale(""));\n wstring s[] = {L"\xe7\x9f\xad3\xe3\x83\x9e231\xe3\x83\xbc\xe2\x97\x8b",L"\xe3\x81\xae\xe3\x81\x8d3\xe3\x83\xbc\xe3\x83\x8a",L"\xe3\x81\x90\xe3\x83\x9e",L"\xe3\x82\x8d\xe3\x81\xab\xe3\x83\x88"};\n for(int i=0;i<4;i++) wcout << setw(10) << s[i] <<123<< endl;\n}\nRun Code Online (Sandbox Code Playgroud)\n会像:
\n \xe7\x9f\xad3\xe3\x83\x9e231\xe3\x83\xbc\xe2\x97\x8b123\n \xe3\x81\xae\xe3\x81\x8d3\xe3\x83\xbc\xe3\x83\x8a123\n \xe3\x81\x90\xe3\x83\x9e123\n \xe3\x82\x8d\xe3\x81\xab\xe3\x83\x88123\nRun Code Online (Sandbox Code Playgroud)\n但如果文本仅包含 ASCII 字符,则它可以正常工作。
\n我知道我可以自己编写一个新函数来对齐它,但我想知道是否已经有一个可靠的解决方案。
\n因此,我正在创建一个小型太阳系模拟,并使用 C 语言完成微积分。计算完成后,我将它们放入一个 json 文件中,该文件由网页读取。
我创建了一个函数将轨迹的坐标保存到json文件中,所有坐标都是双精度的。为了将 double 打印到我的 json 文件中,我使用 fprintf 和 %e (是的,我想要科学写作)。
FILE *fp = NULL;
fp = fopen("file.json", "w");
fprintf(fp, "[[%e, %e, %e],[%e, %e, %e], %d],\n", pos.x, pos.y, pos.z, speed.x, speed.y, speed.z, time);
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我在 main.c 中使用该函数时,它会按照我想要的方式用点写入双精度数(例如:3.14),但是当我在 Gtk 中使用该函数时,它会用逗号写入它们(例如:3,14 )。
我尝试使用 GTK 的 g_fprintf 内置 fprintf 函数,但我遇到了同样的问题。知道如何解决这个问题吗?
(如果您需要有关代码或问题的更多信息,我随时为您服务)
我正在尝试编写以下函数,以便可以将其与Text.Printf.printf的可变数量的参数一起使用。我尝试使用类型类,但找不到任何不逐个字符和逐个参数处理格式字符串的示例。我希望能够捕获所有参数并将其立即传递给 printf 。
-- panic :: PrintfType r -> Int -> String -> r
panic lvl fmt =
let fail :: String -> String -> a
fail s s' = error $ printf "%s: %s" s s'
in flip fail (show lvl) . printf fmt
Run Code Online (Sandbox Code Playgroud)
我想使用这样的函数:
panic 0 "Err" -- Exception: "Err: 0"
panic 1 "Err %s %d" "fatal" 42 -- Exception: "Err fatal 42: 1"
Run Code Online (Sandbox Code Playgroud)
我应该如何继续实施?
printf ×10
c ×5
c++ ×1
cancellation ×1
f# ×1
gtk ×1
haskell ×1
highlighting ×1
notepad++ ×1
php ×1
polyvariadic ×1
printing ×1
pthreads ×1
size-t ×1
str-replace ×1
winmain ×1
wstring ×1