It might sound stupid to most of you.....but it is the first time i want to write 2 columns of data in a txt file.....The first column will be an index of the second column. The second column has constant length but the index will have length from 1 to 4 digits(1-5000) and i would like to know what should i do or use in order to have a constant size for the first column(say 4 digits).I would like to have 2 "tidy" columns

say i have

ofstream file;
file.open("text.txt");
............
code
............
file<<index<<data[index]<<endl;

file.close();

where index is a loop counter and data is an array.

Thanks in advance mates

Recommended Answers

All 3 Replies

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

int main()
  {
  for (int n = 1; n < 6; n++)
    cout << n;
  cout << setw( 5 ) << right << (167) << "mm\n";
  return 0;
  }

Hope this helps.

use setw() to force column width. cout << setw(2) << 12 << setw(6) << "Hello";

thanks mates...

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.