这个问题得到了回答。与格式化程序无关,而是在复制到新缓冲区时我的白痴。
我希望这是一个单行的答案。我有一个snprintf()
类似于以下内容的声明:
snprintf(buffer, sizeof(buffer), "%03d", 0U);
我期待buffer
持有000
,但由于某种原因它只持有00
。假设缓冲区足够大,可以容纳我想要的。我是不是很傻?
编辑:
请参阅下面的完整代码与上下文。我之前试图简化它,因为我认为所有这些上下文都是不必要的。重点仍然存在,使用在第一个 CSV 行中%04u
给了我。只给我。000
%03u
00
uint16_t CSVGenerator::write_csv_data(TestRecord* record)
{
// Define the templates.
const char *row_template = "%04u,%6.3E,%6.3E,%6.3E,%6.3E,%6.3E\n";
char csv_row_buffer[CSV_ROW_BUFFER_SIZE];
// Add the data.
uint16_t row_count = 0U;
for (uint16_t reading = 0U; reading < MEASURE_READING_COUNT; ++reading)
{
// Parse the row.
snprintf(csv_row_buffer, sizeof(csv_row_buffer), row_template,
// Test ID
MEASURE_PERIOD_SECS * reading,
// Impedances Z1-Z5.
record->measurements[reading][0U],
record->measurements[reading][1U],
record->measurements[reading][2U],
record->measurements[reading][3U],
record->measurements[reading][4U]);
// Add it to the main buffer, excluding the terminator.
strncpy((m_csv_data_buffer + (reading * CSV_ROW_BUFFER_SIZE) - 1U),
csv_row_buffer, (sizeof(csv_row_buffer) - 1U));
// Increment the row count.
++row_count;
} // for : each reading.
return row_count;
}