Hi,

I am writing output to a file and am using ostream. I wanted to format my output and not sure how to do it.
Here is some simple code.

int var1;
int var2;
ofstream out("blah.txt");
out << "Name " << var1 << endl;
out << "NameofAnotherPerson << var2 << endl;

How would i get the output in the file that I write into, to be formatted where var1 and var2 line up like in the same column like a printf statement would do?

Recommended Answers

All 2 Replies

setw( size_of_field)

try this:

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

int main()
{
    string name1 = "Alvin";
    string name2 = "Edward";
    cout << left << setw(20) << "Name: " << name1 << "\n";
    cout << setw(20) << "Another Name: " << name2 << "\n";

}
commented: Is that your real name? =P +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.