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

2

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

So that works for the case of ints, but what about aligning doubles about the decimal point instead of the first number?
Nvm figured it out. Just have to align it about the last number. Silly me. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.