标签: printf

C++的String.Format

寻找像.NET的String.Format这样的函数的C++实现.显然有printf和它的品种,但我正在寻找一些位置如下的东西:

String.Format("你好{0}.你已经{1}岁了.感觉{1}怎么样?",姓名,年龄);

这是必要的,因为我们将尝试更轻松地本地化我们的应用程序,并将翻译器{0}和{1}放在句子中的任何位置比给他们%s,%d,%要容易得多d必须按其顺序排列.

我认为搜索和替换变量输入(va_start,va_end等)是我最终要构建的,但如果已经有一个可靠的解决方案,那将是首选.

谢谢 :)

c++ string.format printf localization

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

如何使用printf显示off_t,nlink_t,size_t和其他特殊类型?

在我的程序中,我统计他们想要的文件并发送数据.stat的字段struct都是特殊类型:

struct stat {
  dev_t     st_dev;     /* ID of device containing file */
  ino_t     st_ino;     /* inode number */
  mode_t    st_mode;    /* protection */
  nlink_t   st_nlink;   /* number of hard links */
  uid_t     st_uid;     /* user ID of owner */
  gid_t     st_gid;     /* group ID of owner */
  dev_t     st_rdev;    /* device ID (if special file) */
  off_t     st_size;    /* total size, in bytes */
  blksize_t st_blksize; /* blocksize for file system I/O */
  blkcnt_t  st_blocks;  /* number …
Run Code Online (Sandbox Code Playgroud)

c linux printf portability

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

如何填充printf以考虑负号和可变长度数?

我试图在日志文件中输出一些数字,我想通过printf函数填充一大堆浮点数来产生:

 058.0
 020.0
 038.0
-050.0
 800.0
 150.0
 100.0
Run Code Online (Sandbox Code Playgroud)

目前我这样做:

printf("% 03.1f\n", myVar);
Run Code Online (Sandbox Code Playgroud)

...其中myVar是一个浮点数.该语句的输出如下所示:

58.0
20.0
38.0
-50.0
800.0
150.0
100.0
Run Code Online (Sandbox Code Playgroud)

根据我的阅读,我希望我的代码能够产生我在本文顶部提到的输出,但显然有些不对劲.你一次只能使用一面旗帜吗?..或者还有其他什么东西在这里?

c floating-point formatting printf

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

<inttypes.h>的好介绍

我想建议使用的<inttypes.h>做给别人printf用混合32/64位版本.我尝试了谷歌的介绍或教程页面,其中包含一些示例和使用指南,但我找不到一个.

有人可以推荐一个介绍或教程<inttypes.h>吗?

c printf c99

27
推荐指数
2
解决办法
2万
查看次数

为什么printf()将float提升为double?

从上一个问题:

如果你试图传递floatprintf它,它会doubleprintf收到它之前被提升

printf()是一个可变函数吧?可变参数函数在传递之前是否会提升float参数double

c c++ floating-point printf variadic-functions

27
推荐指数
4
解决办法
3193
查看次数

printf(s)和printf("%s",s)之间的根本区别是什么?

问题很简单,s是一个字符串,我突然想到试图用printf(s)它来查看它是否可行,我在一个案例中得到警告而在另一个案例中没有警告.

char* s = "abcdefghij\n";
printf(s);

// Warning raised with gcc -std=c11: 
// format not a string literal and no format arguments [-Wformat-security]

// On the other hand, if I use 

char* s = "abc %d efg\n";
printf(s, 99);

// I get no warning whatsoever, why is that?

// Update, I've tested this:
char* s = "random %d string\n";
printf(s, 99, 50);

// Results: no warning, output "random 99 string".
Run Code Online (Sandbox Code Playgroud)

那么,什么是之间的基本差异printf(s),并printf("%s", s)和为什么会在短短的一个案例一个警告?

c string printf compiler-warnings format-specifiers

27
推荐指数
3
解决办法
2791
查看次数

在嵌入式C中使用带有sprintf()的浮点数

大家好,我想知道float变量是否可以在sprintf()函数中使用.

就像,如果我们写:

sprintf(str,"adc_read = %d \n",adc_read);
Run Code Online (Sandbox Code Playgroud)

where adc_read是整数变量,它将存储字符串

"adc_read = 1023 \n"

str(假设 adc_read = 1023)

如何使用float变量代替整数?

c embedded floating-point printf

26
推荐指数
2
解决办法
13万
查看次数

在C中打印字符及其ASCII码

如何在C中打印char及其等效的ASCII值?

c printf

26
推荐指数
2
解决办法
30万
查看次数

将void*转换为std :: string

在仔细阅读网页并弄乱自己之后,我似乎无法将void*的目标(这是一个字符串)转换为std :: string.我尝试使用此页面的sprintf(buffer, "%p", *((int *)point));建议来获取C字符串,但无济于事.遗憾的是,是的,我必须使用void*,因为这是SDL在其USEREVENT结构中使用的.

对于那些感兴趣的人,我用来填充Userevent的代码是:

std::string filename = "ResumeButton.png";
SDL_Event button_press;
button_press.type = BUTTON_PRESS;
button_press.user.data1 = &filename;
SDL_PushEvent(&button_press);
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

编辑:感谢所有的回复,我只需要将void*转换为std :: string*.傻我.非常感谢你们!

c++ string printf void-pointers

26
推荐指数
3
解决办法
3万
查看次数

C++ Streams与C风格的IO?

我编码一些C++的小爱好的项目时,我注意到,我使用C风格的操作来访问IO( ,printf,fopen等).

在C++项目中涉及C函数是否被认为是"不良做法"?使用流而不是C风格的IO访问有什么好处?

c++ io printf iostream

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