Organizing file output

strongton used Ask the Experts™
I have a class that outputs info to a file using << operator and sometimes the data is alinged in their respective columns and sometimes depending on the data length they are not.

Store Number: 11
Store Name  : la brea
Date      Item  Description     Qty   Each   Line Total
20020304  139   Pencils/dozen   3     $0.86   2.58
20020301  135   Paperclips/Box   412     $1.19   490.28
Total                                         492.86

it there a simple way of organizing the output
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Do you mean that you want the output file to have nice columns?

Simplest way I think is to use the tab
fout your_data << "\t" << your_data <<"\t"<<your_data<<endl;

Of course, you will need to take a guess as to whether one tab will be long enough.

Also if you are working with a column where the length is known, ie date. then don't use a tab, use a fixed number of spaces.

#include <iostream>
#include <iomanip>
using namespace std;

cout << setw(width_of_field_1) << field_1
        << setw(width_of_field_2) << field_2
        << endl;

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial