user3063349
user3063349

Reputation: 96

c++: print a string with specified length

In C++, using printf-s I want to write a char array (etc char asd[50]) to console with a specified 50 space (similar like "%.2d" method at decimals, if the string shorter fill it with spaces....)

Tried %50s and %.50s methods, both of them wronged my charachters....

I can (hardly) accept answers, but then calculat with the fact, i use a charachter array, so its not wrok to cout<

Upvotes: 0

Views: 3993

Answers (2)

user3063349
user3063349

Reputation: 96

A used a for loop with printfs (using %c) to write it, and after the \0 charachter it write spaces , so problem solved

for(int j=0;j<50;j++)
        {
            printf("%c",asd[j]);
        }

Upvotes: 1

Daniel
Daniel

Reputation: 2185

Try this if you find it useful,

#include <iomanip>
void prints(const char * s)
{
    cout << std::setfill('0') << std::setw(50) << s;
}

Upvotes: 2

Related Questions