D
dwgoldfarb
Hi,
I am writing an application which generates a Comma Separated Values
file with an unknown number of columns at compile time, but they will
be determined at the beginning of runtime prior to outputing the first
line. I could do something like:
int a, b, c, d, e, f;
char a_print, b_print, c_print, d_print, e_print, f_print; /* These
are set at the beginning of runtime. 1=print the value, 0 = no print
*/
main() {
/* These would be set in practice by CommandLine options -- this is
example */
a_print=c_print=e_print =1;
b_print=d_print=f_print=0;
while (1) {
/* get some data, process it, and set variables a,b.c,d,e,f as
needed */
if(a_print) printf("%d,"a);
if(b_print) printf("%d,"b);
if(c_print) printf("%d,"c);
if(d_print) printf("%d,"d);
if(e_print) printf("%d,"e);
if(f_print) printf("%d,"f);
printf("\n");
}
}
But this is slow as it will need to check a_print, b_print .... for
every line of output (potentially millions), and there will be one
call to printf for each column.
I have thought of constructing a printf format string "%d,%d,%d\n"
called "fmt" before the beginning of the while loop, and then call one
invocation of vprintf. The question I have is how to get a "va_list"
char fmt[2000]; /* probably be dynamically allocated, but this is
easier for example */
int a, b, c, d, e, f;
char a_print, b_print, c_print, d_print, e_print, f_print; /* These
are set at the beginning of runtime. 1=print the value, 0 = no print
*/
va_list ap; /* Or similar */
main() {
/* These would be set in practice by CommandLine options -- this is
example */
a_print=c_print=e_print =1;
b_print=d_print=f_print=0;
/* Setup format */
fmt[0]='\0';
if(a_print) strcat(fmt,"%d,");
if(b_print) strcat(fmt,"%d,");
if(c_print) strcat(fmt,"%d,");
if(d_print) strcat(fmt,"%d,");
if(e_print) strcat(fmt,"%d,");
if(f_print) strcat(fmt,"%d,");
strcat(fmt,"\n");
/* setup va_list ap ....here is where I am stumped */
while (1) {
/* get some data, process it, and set variables a,b.c,d,e,f as
needed */
vprintf(fmt,ap); /* ap points to the list of variables a,c,e
*/
}
}
Probably not possible, but any suggestions welcome....thanks
I am writing an application which generates a Comma Separated Values
file with an unknown number of columns at compile time, but they will
be determined at the beginning of runtime prior to outputing the first
line. I could do something like:
int a, b, c, d, e, f;
char a_print, b_print, c_print, d_print, e_print, f_print; /* These
are set at the beginning of runtime. 1=print the value, 0 = no print
*/
main() {
/* These would be set in practice by CommandLine options -- this is
example */
a_print=c_print=e_print =1;
b_print=d_print=f_print=0;
while (1) {
/* get some data, process it, and set variables a,b.c,d,e,f as
needed */
if(a_print) printf("%d,"a);
if(b_print) printf("%d,"b);
if(c_print) printf("%d,"c);
if(d_print) printf("%d,"d);
if(e_print) printf("%d,"e);
if(f_print) printf("%d,"f);
printf("\n");
}
}
But this is slow as it will need to check a_print, b_print .... for
every line of output (potentially millions), and there will be one
call to printf for each column.
I have thought of constructing a printf format string "%d,%d,%d\n"
called "fmt" before the beginning of the while loop, and then call one
invocation of vprintf. The question I have is how to get a "va_list"
char fmt[2000]; /* probably be dynamically allocated, but this is
easier for example */
int a, b, c, d, e, f;
char a_print, b_print, c_print, d_print, e_print, f_print; /* These
are set at the beginning of runtime. 1=print the value, 0 = no print
*/
va_list ap; /* Or similar */
main() {
/* These would be set in practice by CommandLine options -- this is
example */
a_print=c_print=e_print =1;
b_print=d_print=f_print=0;
/* Setup format */
fmt[0]='\0';
if(a_print) strcat(fmt,"%d,");
if(b_print) strcat(fmt,"%d,");
if(c_print) strcat(fmt,"%d,");
if(d_print) strcat(fmt,"%d,");
if(e_print) strcat(fmt,"%d,");
if(f_print) strcat(fmt,"%d,");
strcat(fmt,"\n");
/* setup va_list ap ....here is where I am stumped */
while (1) {
/* get some data, process it, and set variables a,b.c,d,e,f as
needed */
vprintf(fmt,ap); /* ap points to the list of variables a,c,e
*/
}
}
Probably not possible, but any suggestions welcome....thanks