Skip to content

Commit

Permalink
generic function to write table
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfrantz committed Jan 31, 2024
1 parent 17ba24e commit 8fee771
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/cross-level/table-cl.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,47 @@ int width, *max_width = NULL;
}


/** This function writes a table.
--- table: table
--- fname: filename
--- separator: column separator
+++ Return: void
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**/
void write_table(table_t *table, char *fname, char *separator){
int row, col;
FILE *fp = NULL;


if ((fp = fopen(fname, "w")) == NULL){
printf("unable to open file %s\n", fname);
exit(FAILURE);
}


if (table->has_col_names){

if (table->has_row_names) printf("rowname%s", separator);

for (col=0; col<(table->ncol-1); col++) printf("%s%s", table->col_names[col], separator);
printf("%s\n", table->col_names[col]);

}

for (row=0; row<table->nrow; row++){

if (table->has_row_names) printf("%s%s", table->row_names[row], separator);
for (col=0; col<(table->ncol-1); col++) printf("%.2f%s", table->data[row][col], separator);
printf("%.2f\n", table->data[row][col]);

}


fclose(fp);

return;
}


/** This function frees a table.
--- table: table
+++ Return: void
Expand Down
1 change: 1 addition & 0 deletions src/cross-level/table-cl.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ table_t read_table(char *fname, bool has_row_names, bool has_col_names);
table_t allocate_table(int nrow, int ncol, bool has_row_names, bool has_col_names);
void init_table(table_t *table);
void print_table(table_t *table, bool truncate);
void write_table(table_t *table, char *fname, char *separator);
void free_table(table_t *table);

#ifdef __cplusplus
Expand Down

0 comments on commit 8fee771

Please sign in to comment.