Reputation: 1
I am using write
to create a csv file with the following type of values on every line
int16_t, int16_t, int16_t, int64_t, uint64_t
First a buffer is filled using sprintf and the it is passed to write. However, there is only one line with all the values in the written file. No new line.
static char line[34];
sprintf(line, "%d,%d,%d,%ld,%lu\n", ...);
write(fd_csv_data, line, sizeof(line));
%d,%d,%d,%ld,%lu makes 32 bytes in total, adding \n and \0 results in 34. What am I doing wrong ?
Upvotes: 0
Views: 581
Reputation: 11406
Your buffer could overflow, so you'll have to calculate the maximum size of the generated string or just use a buffer big enough.
To write to the file, you can use the return value of sprintf()
:
static char line[256];
int n = sprintf(line, "%d,%d,%d,%ld,%lu\n", ...);
write(fd_csv_data, line, n);
As an alternative the safer snprintf()
could be used.
With some extra checks:
#define LINESIZE 256
static char line[LINESIZE];
int n = sprintf(line, "%d,%d,%d,%ld,%lu\n", ...);
if (n > 0 && n < LINESIZE) {
write(fd_csv_data, line, n);
}
// else..
Upvotes: 0
Reputation: 409216
Two problems:
To solve both these issues, use strlen
instead to get the actual length of the string:
write(fd_csv_data, line, strlen(line));
On another couple of notes:
snprintf
instead of sprintf
, to avoid possible buffer overrunsl
might be wrong for 64-bit types, use the standard format macro constants, like PRId64
for int64_t
.Upvotes: 2