寻找像.NET的String.Format这样的函数的C++实现.显然有printf和它的品种,但我正在寻找一些位置如下的东西:
String.Format("你好{0}.你已经{1}岁了.感觉{1}怎么样?",姓名,年龄);
这是必要的,因为我们将尝试更轻松地本地化我们的应用程序,并将翻译器{0}和{1}放在句子中的任何位置比给他们%s,%d,%要容易得多d必须按其顺序排列.
我认为搜索和替换变量输入(va_start,va_end等)是我最终要构建的,但如果已经有一个可靠的解决方案,那将是首选.
谢谢 :)
在我的程序中,我统计他们想要的文件并发送数据.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) 我试图在日志文件中输出一些数字,我想通过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)
根据我的阅读,我希望我的代码能够产生我在本文顶部提到的输出,但显然有些不对劲.你一次只能使用一面旗帜吗?..或者还有其他什么东西在这里?
我想建议使用的<inttypes.h>做给别人printf用混合32/64位版本.我尝试了谷歌的介绍或教程页面,其中包含一些示例和使用指南,但我找不到一个.
有人可以推荐一个介绍或教程<inttypes.h>吗?
从上一个问题:
如果你试图传递
float给printf它,它会double在printf收到它之前被提升
printf()是一个可变函数吧?可变参数函数在传递之前是否会提升float参数double?
问题很简单,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)和为什么会在短短的一个案例一个警告?
大家好,我想知道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变量代替整数?
在仔细阅读网页并弄乱自己之后,我似乎无法将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++的小爱好的项目时,我注意到,我使用C风格的操作来访问IO( ,printf,fopen等).
在C++项目中涉及C函数是否被认为是"不良做法"?使用流而不是C风格的IO访问有什么好处?
printf ×10
c ×7
c++ ×4
string ×2
c99 ×1
embedded ×1
formatting ×1
io ×1
iostream ×1
linux ×1
localization ×1
portability ×1