1

I'm trying to print text to an output file, and I need my output file to match the proper formatting exactly.

I need to do this so that when my program's output is compared to what the proper output should be, (talking large output files here) a simple string comparison of the output files should flag them as perfectly identical.

Here is an example of the EXACT expected output:

| Item   1234 | CALCULATOR           | $   0.45 |
| Item   5678 | USA_FLAG             | $  10.99 |
| Item   9012 | WITCH_BROOM          | $  18.00 |

Notice how with this formatting there are variable amounts of spaces after each string and before the double numbers, but they still manage to line up perfectly. So how do you output this kind of formatting?

I'm assuming there is something about fprintf() that I just don't know, but again, I don't know soooooooooooooo

Anon
  • 99
  • 7

1 Answers1

2

You can specify flags and width in the printf format string e.g.

printf( "%10s", "test" ) will print a 10 characters and pad with spaces if the argument is shorter. e.g (dots added for spaces)

test......

You can also specify justification e.g.

printf( "%-10s", "test" )

......test

jxh
  • 69,070
  • 8
  • 110
  • 193
Nick d'Alterio
  • 166
  • 1
  • 6
  • So that works for the case of ints, but what about aligning doubles about the decimal point instead of the first number? – Anon Feb 16 '19 at 01:08
  • Nvm figured it out. Just have to align it about the last number. Silly me. Thank you! – Anon Feb 16 '19 at 01:11