标签: printf

用C解码printf语句(Printf Primer)

我正在努力将一些旧代码从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?

c printf qstring qt

4
推荐指数
2
解决办法
3282
查看次数

printf似乎忽略了字符串精度

所以,我有点受阻.根据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 …

c printf string-formatting

4
推荐指数
1
解决办法
4466
查看次数

在Matlab中将单元格数组打印为.txt

我有一个单元格数组需要根据特定格式打印在.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行.没有行会有空列.矢量化解决方案是否可行?应该感谢你的帮助.

matlab printf text-files

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

填充计数器前导0到9,然后用PHP删除前导零

用前导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)

php printf count

4
推荐指数
1
解决办法
2089
查看次数

printf的格式化缓冲区在哪里?

我在一个受限制的嵌入式系统上工作.

目前我们使用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函数可以使用字符缓冲区进行格式化,例如整数或浮点到文本.

问题:

  1. printf用于格式化的缓冲区在哪里?(在我做出改变之前,其他有探究的人都想知道.)
  2. 这是编译器(平台)依赖的问题吗?

平台:Arm7tdmi处理器,片上系统(SOC),IAR EW编译器.

c c++ embedded printf iar

4
推荐指数
1
解决办法
420
查看次数

理解fprintf语法

我对C知之甚少,但我正在努力理解用它编写的程序.

我遇到过这一行,但我不确定这意味着什么:

fprintf(kinsh.fp, "%-.*lg", ndigits, kins[i+j*n]);
Run Code Online (Sandbox Code Playgroud)

我认为这意味着"将带有ndigits小数位或有效数字(以较短者为准)的数字打印到kinsh(这是一个文件).但是,我不知道哪个kins,这是一个数组,正在那里做.

我有没有误解-在上下文中,它会如果线路是从阅读更有意义kinsh和写入金穗卡?

c printf

4
推荐指数
1
解决办法
141
查看次数

我可以使用printf()打印该类的对象吗?

我有一些像这样的代码:

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)

c++ printf cout

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

什么是(ptr - A [0])/(sizeof(A [0])/ sizeof(A [0] [0]))的类型?

我有一个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没有丑陋的演员?

c c++ arrays printf

4
推荐指数
1
解决办法
87
查看次数

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)

c printf

4
推荐指数
1
解决办法
535
查看次数

找到$(printf ... array)条件

我正在尝试使用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)

但这当然没有用,因为某些目录名中有空格要排除.此外,我真的不明白为什么会有任何区别.

bash printf find

4
推荐指数
1
解决办法
68
查看次数

标签 统计

printf ×10

c ×6

c++ ×3

arrays ×1

bash ×1

count ×1

cout ×1

embedded ×1

find ×1

iar ×1

matlab ×1

php ×1

qstring ×1

qt ×1

string-formatting ×1

text-files ×1