I'm currently creating a program that takes a CSV file, parses it, then prints out the various items on the page. Eventually, you'd have various columns of data. In order to have the columns looking nicely, you'd have to add padding to the fprintf()
statement (at least I can't think of another way of doing it). The below is a snippet from my code:
for (int pos = 0; pos < size; pos++) {
fprintf(stdout, "%30s - %20d, %20d, %20s, %20d, %20d\n",
(char *)(item+pos)->itemval, (item+pos)->column,
(item+pos)->row, (char *)(item+pos)->columntitle,
(item+pos)->adjustedpos, (item+pos)->adjustedcolumn,
(item+pos)->adjustedrow);
}
Is it possible to use a macro constant as the padding? For example, in the printf()
statement, can I have something like fprintf(..., "%'COLUMNMACROCONSTANT's", ...)
, which is defined in a macro? I apologize if this question has been asked before, but I don't know how to describe that part of the fprintf()
function. I tried to just put it in, but it won't let me, which makes sense (how does the compiler know to separate the macro from the string). I'm wondering if there's some kind of separator like "\"
for macros contained in this area of the print function.
*
such as in sayprintf("%*d", width, value);
printf("%-*s", width, text);
printf("%*d", width, value);
to enable left justification (the-
flag) together with a (positive) field width.(item+pos)->column
, it is more idiomatic to writeitem[pos].column
. (Functionally they are completely equivalent, of course.)