Sponsored Content
Top Forums Programming Passing printf formatting parameters as variables Post 302888882 by Don Cragun on Tuesday 18th of February 2014 12:20:20 AM
Old 02-18-2014
You say case 3 should print white characters on a white background, but using color code 0 (as given in your sample code) prints BLACK characters on a white background.

Have you considered making your output() function use variable arguments like printf()? In the example below, output() uses the last character in the given format string argument to determine the type of the value to be retrieved from the argument list and passed on to printf(). It sets the selected color, uses the format given to print the final argument, and then resets the color back to the defaults.

Code:
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char    format[128];    // internal printf format string
int     colors[] = {33/*yellow*/, 32/*green*/, 31/*red*/, 0/*black*/};

int
output(int clr, const char *fmt, ...) {
        va_list         ap;             // variable arguments list
        double          argd;           // double from argument list
        int             argi;           // int from argument list
        const char *    args;           // pointer to string from argument list
        char            cc;             // conversion character from fmt
        int             rc = 0;         // return code

        va_start(ap, fmt);
        cc = fmt[strlen(fmt) - 1];
        snprintf(format, sizeof(format), "\e[%dm%s\e[0m", colors[clr], fmt);
        switch(cc) {
        case 'd':       argi = va_arg(ap, int);
                        printf(format, argi);
                        break;
        case 'f':       argd = va_arg(ap, double);
                        printf(format, (float)argd);
                        break;
        case 's':       args = va_arg(ap, const char *);
                        printf(format, args);
                        break;
        default:        rc = 1;
                        break;
        }
        va_end(ap);
        return(rc);
}

int
main(int argc, char *argv[]) {
        int     APM;    // input APM
        float   AMBPM;  // input AMBPM
        int     Count;  // input count
        FILE    *fp;    // Input file pointer
        char    Hour[3];// input hour
        float   MBytes; // input MBytes
        int     n;      // # items read from input line

        if((fp = fopen(argc > 1 ? argv[1] : "file", "r")) == NULL) {
                snprintf(format, sizeof(format), "%s: fopen(%s) failed",
                        argv[0], argc > 1 ? argv[1] : "file");
                perror(format);
                exit(1);
        }
        printf("Hour Count APM AMBPM  MBytes\n");
        printf("----+-----+---+-----+-------\n");
        while((n = fscanf(fp, "%2s,%d,%d,%f,%f\n",
                Hour, &Count, &APM, &AMBPM, &MBytes)) != EOF) {
                output(0, "%4s", Hour);
                output(3, " %5d", Count);
                output(1, " %3d", APM);
                output(2, " %5.2f", AMBPM);
                output(3, " %7.2f", MBytes);
                printf("\n");
        }
        exit(0);
}

This certainly isn't robust code and just uses different colors for the different output columns (instead of ranges of values within columns), but it may help you see an alternative approach.

With an input file named file containing:
Code:
00,485,8,0.64,38.45
01,511,9,0.06,3.51
02,517,9,0.07,4.43

it produces the output:
Code:
Hour Count APM AMBPM  MBytes
----+-----+---+-----+-------
  00   485   8  0.64   38.45
  01   511   9  0.06    3.51
  02   517   9  0.07    4.43

 

10 More Discussions You Might Find Interesting

1. Answers to Frequently Asked Questions

Passing variables/arguments/parameters to commands

A good place to start is simple variable passing.... Passing variables from one script to another The next level is passing a variable into a more complex command such as using a variable in a sed command. There are some simple quoting techniques that are very general. These are mentioned... (0 Replies)
Discussion started by: Perderabo
0 Replies

2. Shell Programming and Scripting

passing more than 9 parameters

hi, i am passing around 14 parameters for a script a=$1 b=$2 c=$3 d=$4 e=$5 f=$6 g=$7 h=$8 i=\"${9}\" shift j=\"${1}\" still for j it is displaying the 1st parameter value..how to make it take the 10th parameter (2 Replies)
Discussion started by: dnat
2 Replies

3. Shell Programming and Scripting

awk printf formatting using string format specifier.

Hi all, My simple AWK code does C = A - B If C can be a negative number, how awk printf formating handles it using string format specifier. Thanks in advance Kanu :confused: (9 Replies)
Discussion started by: kanu_pathak
9 Replies

4. Shell Programming and Scripting

using AWK printf with userdefine variables

seems simple but i've not been successfull in adding the value of a Variable to a every line of a file using AWK & printf i've come up with the following, but it's not working.:mad: --- mydatafile.dat e.g. of data in file: cau_actvty_fa_lrf.ksh fan_soco_fa.ksh ny_sum_lst.ksh... (4 Replies)
Discussion started by: danmauer
4 Replies

5. Shell Programming and Scripting

Help formatting a string. Something like printf?

Hi I'm having a problem with converting a file: ID X 1 7 1 8 1 3 2 5 2 7 2 2 To something like this: ID X1 X2 X3 1 7 8 3 2 5 7 2 I've tried the following loop: for i in `cat tst.csv| awk -F "," '{print $1}'| uniq`;do grep -h $i... (4 Replies)
Discussion started by: flotsam
4 Replies

6. UNIX for Dummies Questions & Answers

printf formatting

Is there a way to make these 2 numbers - $482477.37 and $1875000.00 look like $482,477.37 and $1,875,000.00 with printf? (4 Replies)
Discussion started by: nickg
4 Replies

7. Shell Programming and Scripting

String formatting using awk printf

Hi Friends, I am trying to insert lines of the below format in a file: # x3a4914 Joe 2010/04/07 # seh Lane 2010/04/07 # IN01379 Larry 2010/04/07 I am formatting the strings as follows using awk printf: awk 'printf "# %s %9s %18s\n", $2,$3,$4}' ... (2 Replies)
Discussion started by: sugan
2 Replies

8. Shell Programming and Scripting

assinging the printf result to variables

Getting the below error while executing this. Able to run the below commands Individually. #!/bin/bash a=$(printf "%d\n" 0x01E); b=$(printf "%d\n" 0x01A); echo $a echo $b c=`expr $a - $b` echo $c syntax error at line 2: `a=$' unexpected (2 Replies)
Discussion started by: sai_1712
2 Replies

9. Shell Programming and Scripting

Text Formatting using printf[ksh]

Hi All, I am attempting to create a fixed length tilde delimited file using printf. The variables are initialized to fixed length blank spaces a=' ' b=' ' c=' ' d=' ' Sometimes the variable might contain values and sometimes they are... (5 Replies)
Discussion started by: angie1234
5 Replies

10. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies
All times are GMT -4. The time now is 06:13 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy