我正在努力将一些旧代码从1998年带到21世纪.该过程的第一步是将printf语句转换为QString变量.不管我多少次回头看看printf,我总是忘记了一件事.所以,为了好玩,让我们一起解码它,为了时间的缘故,并在此过程中为Stackoverflow 创建第一个小'printf primer'.
在代码中,我遇到了这个小宝石,
printf("%4u\t%016.1f\t%04X\t%02X\t%1c\t%1c\t%4s", a, b, c, d, e, f, g);
Run Code Online (Sandbox Code Playgroud)
如何格式化变量a,b,c,d,e,f,g?
所以,我有点受阻.根据man 3 printf我的系统,字符串格式"%5s"应使用指定的精度来限制从给定的字符串参数打印的字符数.
% man 3 printf
PRINTF(3) BSD Library Functions Manual PRINTF(3)
NAME
printf, fprintf, sprintf, snprintf, asprintf, vprintf, vfprintf,
vsprintf, vsnprintf, vasprintf -- formatted output conversion
...
s The char * argument is expected to be a pointer to an array of
character type (pointer to a string). Characters from the array
are written up to (but not including) a terminating NUL charac-
ter; if a precision is specified, no more than the number … 我有一个单元格数组需要根据特定格式打印在.txt文件中.我已经尝试了一些在线帮助(包括matlab中心,dlmcell但即使这样也没有给我想要的答案.Delimiter是\ t.
cellarray = { ...
'AAPL' '2/20/2011' 100.5
'MSFT' '2/15/2011' 43.4551
} ;
Run Code Online (Sandbox Code Playgroud)
输出应该是.txt文件,格式如下:(使用制表符分隔符)
"AAPL" "2/20/2011" 100.5
"MSFT" "2/15/2011" 43.4551
Run Code Online (Sandbox Code Playgroud)
单元格最少有8000行,最多15000行.没有行会有空列.矢量化解决方案是否可行?应该感谢你的帮助.
用前导0填充至9,然后移除.
01,02,03,04,05,06,07,08,09 - 10,11,12,14
到目前为止我有这个:
<?php $value = $count++;
printf("%02d", $value);
?>
Run Code Online (Sandbox Code Playgroud) 我在一个受限制的嵌入式系统上工作.
目前我们使用snprintf缓冲区,然后使用另一个语句,将缓冲区打印到串口:
char temp_buffer[256];
int bytes_written = snprintf(temp_buffer, sizeof(temp_buffer),
"Solar system has %d planets\n",
10);
if (bytes_written > 0)
{
Serial_Port_Output(temp_buffer, bytes_written);
}
Run Code Online (Sandbox Code Playgroud)
我想切换到printf直接写入串口.根据我们的编译器文档,我拦截了函数调用以输出数据以使用串行端口.(接口使用块写入:地址和字符数).
该printf函数可以使用字符缓冲区进行格式化,例如整数或浮点到文本.
问题:
printf用于格式化的缓冲区在哪里?(在我做出改变之前,其他有探究的人都想知道.)平台:Arm7tdmi处理器,片上系统(SOC),IAR EW编译器.
我对C知之甚少,但我正在努力理解用它编写的程序.
我遇到过这一行,但我不确定这意味着什么:
fprintf(kinsh.fp, "%-.*lg", ndigits, kins[i+j*n]);
Run Code Online (Sandbox Code Playgroud)
我认为这意味着"将带有ndigits小数位或有效数字(以较短者为准)的数字打印到kinsh(这是一个文件).但是,我不知道哪个kins,这是一个数组,正在那里做.
我有没有误解-在上下文中,它会如果线路是从阅读更有意义kinsh和写入金穗卡?
我有一些像这样的代码:
class Point {
public:
int x,y;
Point() : x(1), y(1) {}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用printf()以下方式打印该类的对象:
int main()
{
Point point;
printf("%o",point);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
或者我必须超载operator<<和使用std::cout:
std::ostream& operator<<(std::ostream& os, Point const& p)
{
os << p.x << "," << p.y;
return os;
}
int main()
{
Point point;
std::cout << point;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个2D数组A和一个指向ptr它内部的指针.我知道如何计算行号,但表达式的实际类型似乎是不可移植的:
#include <stdio.h>
int main(void) {
int A[100][100];
int *ptr = &A[42][24];
printf("row number is %d\n", (ptr - A[0]) / (sizeof(A[0]) / sizeof(A[0][0])));
printf("col number is %d\n", (ptr - A[0]) % (sizeof(A[0]) / sizeof(A[0][0])));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在OS/X上,clang编译器以这种方式抱怨:
warning: format specifies type 'int' but the argument has type 'unsigned long' [-Wformat]
Run Code Online (Sandbox Code Playgroud)
在Linux上,gcc发出类似的警告,但在Windows上,我得到一个不同的诊断:
warning: format specifies type 'int' but the argument has type 'unsigned long long' [-Wformat]
Run Code Online (Sandbox Code Playgroud)
这个表达式的实际类型是什么?
有没有办法将表达式传递给printf没有丑陋的演员?
我正在尝试以常见的日志格式打印一些文本.
printf("%s - - [%s] %s %d %zu\n", ip, _time, row, statuscode, size);
Run Code Online (Sandbox Code Playgroud)
问题是订单混乱了.输出是:
200 1511 - - [20/Sep/2017:13:07:32 +0200] GET / HTTP/1.1
Run Code Online (Sandbox Code Playgroud)
我认为(1511)是ip印刷品.不知道为什么.
当我像这样打印它们时:
printf("1. %s\n", ip);
printf("2. %s\n", _time);
printf("3. %s\n", row);
printf("4. %d\n", statuscode);
printf("5. %zu\n", size);
Run Code Online (Sandbox Code Playgroud)
它像预期的那样工作:
1. 127.0.0.1
2. 20/Sep/2017:13:11:24 +0200
3. GET / HTTP/1.1
4. 200
5. 151
Run Code Online (Sandbox Code Playgroud)
当我statuscode因某些原因添加时,问题似乎开始了.我不知道为什么.任何帮助表示赞赏.
这是我使用prinft()的函数:
static void handlelogging(char* method, struct sockaddr_storage client_addr, size_t size, char* row, int statuscode) {
char* ip; …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用bash目录数组来排除find结果.但是,它没有像我期望的那样工作,我想理解为什么.
例:
mkdir -p findtest
cd findtest
mkdir -p A B C
exclude=(A C)
echo $(printf -- "-not -path '*/%s' " "${exclude[@]}")
Run Code Online (Sandbox Code Playgroud)
这打印-not -path '*/A' -not -path '*/C'.如果按字面意思使用它,它的工作原理:
find . -not -path '*/A' -not -path '*/C'
.
./B
Run Code Online (Sandbox Code Playgroud)
但是使用相同的命令替换,它不会从输出中排除任何目录:
find . $(printf -- "-not -path '*/%s' " "${exclude[@]}")
.
./C
./B
./A
Run Code Online (Sandbox Code Playgroud)
现在,如果我忽略要排除的路径名称周围的引号,它实际上是有效的:
find . $(printf -- "-not -path */%s " "${exclude[@]}")
.
./B
Run Code Online (Sandbox Code Playgroud)
但这当然没有用,因为某些目录名中有空格要排除.此外,我真的不明白为什么会有任何区别.