我正在尝试使用vsprintf()输出格式化的字符串,但我需要在运行之前验证我有正确数量的参数以防止"参数太少"错误.
从本质上来说,我认为我需要的是一个正则表达式来计算类型说明符的数量,但是当涉及到正则表达式时我很无用,而且我无法在任何地方为它提供资金,所以我想我会给它一个去.:)
除非你能想出更好的方法,否则这种方法符合我的要求.
function __insertVars($string, $vars = array()) {
$regex = '';
$total_req = count(preg_match($regex, $string));
if($total_req === count($vars)) {
return vsprintf($string, $vars);
}
}
Run Code Online (Sandbox Code Playgroud)
如果你能想到更简单的方法,请告诉我.
是否可以编写一个返回printf格式的宏(使用标记串联)?例如
#define STR_FMT(x) ...code-here...
Run Code Online (Sandbox Code Playgroud)
STR_FMT(10) 扩展到 "%10s"
STR_FMT(15) 扩展到 "%15s"
...等
这样我就可以在printf中使用这个宏了:
printf(STR_FMT(10), "*");
Run Code Online (Sandbox Code Playgroud) 我有一个问题,我无法掌握printf.这是我第一次遇到这个问题,所以我确定这是天真的,但无论如何,我都无法自己解决......也许只是因为我累了:fprintf(我发现了)它也适用于printf)只能正确打印第一个参数,从第二个参数开始,它只打印"0"表示数字,"(null)"表示字符串
这是相关的代码:
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void printInputStream(MatePair* inputStream, char* leftFile, char* rightFile){
MatePair* iterator = inputStream;
FILE* outLeft = fopen(leftFile, "w");
FILE* outRight = fopen(rightFile, "w");
while (iterator->leftRow != MATEPAIR_STOP){
fprintf(outLeft, "%d: \n", iterator->leftRow);
fprintf(outLeft, "%s \n", iterator->leftDNA);
fprintf(outLeft, "%d: %s \n", iterator->leftRow, iterator->leftDNA);
iterator++;
}
fclose(outLeft);
fclose(outRight);
}
Run Code Online (Sandbox Code Playgroud)
这是输出的开头:
48:
NAATAGACCTATATCCTGTACCCAAACAGAAGACAGAGGATTAACCAAACTCTT
48: (null)
44:
NTAGCCATCTTAGACACATGAATATCTTGGGTCACAACTCATACCTCAACAAAA
44: (null)
40:
NAAAATAAGGGGTATACTCGCTTCGGGGCCCCATTTGGCCTCCAAAAGGGGGCG
40: (null)
36:
NTCTATCTTGCTCGAGAGAAAGGGTTGCCTTAGGGTTTTTTGGGGGGGGCTGTA
36: (null)
32:
NCTATAGAAATTTCCCATACCAACTAGACATTTATCTTCCTGTTTTTTTCCGCC
32: (null)
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我打印了两次数组的每个成员:每个参数一次,两个参数一起.数据很好,实际上第一种方法都可以,第二种方法只打印第一个参数.任何的想法?提前致谢
我的教授要求我的代码每行不超过80个字符,但我有一些超出此限制的printf语句.有没有办法在不改变输出的情况下将此语句分成两行或多行?
请求示例:
printf("\n%-20s %-4d %-20s %-4d %-20s %-4d\n%-20s %-4d %-20s %-4d%-20s %-4d\n%-20s %-4d %-20s %-4d %-20s %-4d\n%-20s %-4d %-20s %-4d %-20s %-4d\n%-20s %-4d %-20s %-4d\n", "1 - Ones", ones, "2 - Twos", twos, "3 - Threes", threes, "4 - Fours", fours, "5 - Fives", fives, "6 - Sixes", sixes, "7 - Three of a Kind", threeOfAKind, "8 - Four of a Kind", fourOfAKind, "9 - Full House", fullHouse, "10 - Small Straight", smallStraight, "11 - Large Straight", largeStraight, "12 - …Run Code Online (Sandbox Code Playgroud) 根据C11章节§7.21.6.1,P9
如果转换规范无效,则行为未定义.282)如果任何参数不是相应转换规范的正确类型,则行为未定义.
到时候,我的理解是,为
char str [] = "Sourav";
Run Code Online (Sandbox Code Playgroud)
printf("%S", str);属于第一句话,没有CS作为%S(UPPERCASE)printf("%d", str);属于第二句(CS和参数类型不匹配,但是%d是不是 "无效" CS,反正)我的理解错了吗?第二个语句是否也可以归类为"无效"(PS-而非"错误")转换说明符?
我在我的C++ GUI应用程序中使用Unicode字符串作为图标,并希望摆脱u8"\uf118"我所散布的所有魔术字符串,并且在途中,使这些字符串成为他们自己的类型.
所以我创建了一个这样的类:
struct icon
{
explicit constexpr icon(const char (&unicode_icon)[4]) :
_icon{ unicode_icon[0], unicode_icon[1], unicode_icon[2], unicode_icon[3] }
{
}
operator const char*() const
{
return _icon.data();
}
private:
std::array<char, 5> _icon;
};
Run Code Online (Sandbox Code Playgroud)
GUI库使用某种形式printf(我认为vfprintf)所以代码调用:
icon smiley { u8"\uf118" };
printf("%s", smiley);
Run Code Online (Sandbox Code Playgroud)
我到目前为止在Windows上对此进行测试并且效果很好,但是当我使用gcc(5.1)在Linux上编译它时,我收到以下警告:
main.cpp:22:24: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'icon' [-Wformat=]
printf("%s", smiley);
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我在内部遇到了一个seg故障,vfprintf因为它没有使用我提供的强制转换运算符.
我知道当我将图标传递给GUI库时,我可以显式地转换图标(它有以下原型,(const char* fmt, ...)但这会让我写更多的代码,而且,它看起来会更糟.
有没有办法让编译器发挥魔力并允许我像这样调用函数:( printf("%s", smiley);或其他一些简单的方法)?
这导致分割错误。需要纠正什么?
int main(void)
{
char a_static = {'q', 'w', 'e', 'r'};
char b_static = {'a', 's', 'd', 'f'};
printf("\n value of a_static: %s", a_static);
printf("\n value of b_static: %s\n", b_static);
}
Run Code Online (Sandbox Code Playgroud) 在即将到来的c考试中,我们有一个问题可以为您提供额外的学分.
问题始终与各种打印类型的棘手语法有关.
总的来说,我理解了我所经历的所有问题,但有两个问题让我感到困惑:
以下程序的输出是什么?
#include <stdio.h>
void main(){
printf ("%c", '&'&'&');
}
Run Code Online (Sandbox Code Playgroud)
回答:&
以下程序的输出是什么?
#include <stdio.h>
#include <string.h>
void main(){
printf("%c",strcmp("***","**")*'*');
}
Run Code Online (Sandbox Code Playgroud)
回答:*
正如您所看到的,问题非常相似.我的问题是,为什么这是输出?
关于第一个问题:我理解一个字符,逻辑方面,总是为TRUE,并且使用TRUE AND-TRUE给你TRUE(或1),但为什么它会将1转换为'&',为什么不呢? ASCII表中的char等价于1?(注意%c的必要打印而不是%d)
关于第二个问题:我理解strcmp根据'首先出现在字典中'的值返回一个int,在这个例子中将导致1但是为什么将它与char'*'相乘(再次,逻辑方面等于1)会导致转换(1*1 = 1)为char'*'?
假设我有一个带有许多参数的printf:
printf("%d %d %d %d", A, B, C, D);
Run Code Online (Sandbox Code Playgroud)
由于某种原因,我希望不再打印一个参数,但仍将其列在参数列表中(例如,出于视觉原因,或者可能是带有必要副作用的函数调用等)。
我可以用无输出的转换字母替换%d吗?我不记得有这样一封转换信。或者也许正在玩旗帜...?
[编辑]我刚刚注意到scanf有类似的东西(但是相反):分配抑制标志'*'。例如sscanf("123 345 678", "%i %*i %i", &a, &b)将导致a = 123 b = 678