How to make variables hold constant number of characters

I have a text file with a bunch of different names and integer values.
Let's say I need to read them and then store in another file giving constant 10 characters for char to hold, and 5 constant places to hold for int values.


In short,
turn something like this:
 
John 2 1 5


into this:
 
John      2    1    5



How to do it the proper way (without cout and empty spaces in brackets :D )?

It seems that
char name[any number];
isn't giving a single empty character in the output.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <iomanip>


int main()
{
    std::string first("Hi"), second("Hello");
    int a1(1), a2(3), a3(5), b1(2), b2(4), b3(8);
    std::cout << std::left;
std::cout << std::setw(10) << first << std::setw(2) << a1 <<
             std::setw(2) << a2 << std::setw(2) << a3 << std::endl;
std::cout << std::setw(10) << first << std::setw(2) << b1 <<
             std::setw(2) << b2 << std::setw(2) << b3 << std::endl;
}
Last edited on
thanks, but what about text file, what would be the proper code meaning


std::ofstream ("b.txt") << std::setw(10)?


EDIT: figured it out:

std::ofstream ("b.txt") << std::left<< std::setw(10)

No idea why you seperated those 2 lines, thanks once again anyway.
Last edited on
Topic archived. No new replies allowed.