Is there a space command in the print statement

Posted on 2002-07-24
Last Modified: 2008-02-01
in using msvca++ 6.0,  I want to write my data to the file in a uniform positions.  let's say that I want to write 15 spaces in a line before I write the first variable on that line.  is there a space command where I tell c to write 15 spaces first before writing the variable? or in the Format statement if I am writing the data to a file?

exsample:  printf("               %s  %d  %d\n", name, age, grade);
CString text;
text.Format("               %s  %d  %d\n", name, age, grade);
Question by:adatd
LVL 17

Expert Comment

by:Barry Cunney
ID: 7174493
Hi adatd,
This may give you some ideas

include "stdafx.h"
#include <fstream.h>
#include <iostream>
#include <iomanip.h>

int main(int argc, char* argv[])

  ofstream SpacesFile ("C:\\Spaces.txt");
     if (SpacesFile.is_open())
      SpacesFile.fill(' ');
      SpacesFile.width(15+2);     // 15 spaces and 2 spaces  for age
      SpacesFile << 42 <<endl;


  return 0;


Also you can use setw which is useful if writing to file in specified columns
ofstream TenforeFeedResults ("C:\\TenforeFeedResults.txt");
if (TenforeFeedResults.is_open())

Accepted Solution

ris earned 50 total points
ID: 7174712
I find it much easier to use the printf() style format specifiers through CString::Format() for all kinds of text alignment like this.  Here is some code with comments to exemplify some options.  For more information, check your favorite C/C++ language reference for the options to printf(), sprintf(), and/or CString::Format()

#include <iostream.h>
#include <afx.h>

void WriteLine(CString sName, int nAge, int nGrade)
    const CString sEmpty(""); //an empty string
    CString sLine; //the line to print

    //%15s with an empty string passed in creates 15 spaces by default
    //%-n causes the data to be left-justified in a field of n width
    //Note that this statement starts the name at the 17th column because
    //there are 15 spaces from the %15s (sEmpty) followed by a space in the
    //format string, and then the name, left-justified in a field 15 chars wide
    //I left the age and grade right-justified because numbers usually look
    //better that way.  But if you must align your data to begin on set columns
    //because it is going to be read as a database by another program or
    //something like that,
    sLine.Format(_T("%15s %-15s %2d %2d"), sEmpty, sName, nAge, nGrade);

    //Alternatively, you can supply the width in an int variable by using
    //this syntax:
    int nNumSpaces = 15; //could be a function parameter of course
    sLine.Format(_T("%*s %-15s %2d %2d"), nNumSpaces, sEmpty, sName, nAge, nGrade);
} //end WriteLine()

void Pause()
    cout<<endl<<"Press Enter to continue...";
    char Enter;

void main()
    WriteLine("John Smith", 12, 1);
    WriteLine("Jane Doe", 7, 12);


Expert Comment

ID: 7174733
Hi adatd,
try this:

int iSpaces = 5;
const char* pszEmptyString = "";

for exsample:
printf("%*s  %d  %d\n", iSpaces, name, age, grade);
or if first item is not string:
printf("%*s  %d  %d\n", iSpaces, pszEmptyString, age, grade);
LVL 30

Expert Comment

ID: 7175547
Another approach would be to use tab charactor.


printf("\t\t%s \t%d \t%d\n", name, age, grade);

Expert Comment

ID: 7175615
The best method to use depends a lot on your purposes.  Are you just trying to create a left margin?  Are you laying out text for another program to process such as Excel or some other database or spreadsheet program?  The question really is: why do you want to indent all the text by 15 spaces?

If you are just trying to create a left margin, then you should use tabs as suggested by Axter.  I only say this because tabs are a more standard way of creating indentation for simple text layout purposes than are spaces, and it will translate better if somebody imports your text file into word processor software like MS Word.

If you need to align data on specific column positions (like for a database) then you should use format specifiers like the "%-15s" or "%*s" syntax in printf() or CString::Format(), or using the iostream functions like ofstream::fill() and ofstream::width().  This kind of approach will give you better programmatic control over column alignment in multiple lines.

I suggest you avoid simply including lots of spaces in your string literals as in your original example code.  It's much more readable to say this:

printf("%15s%s %d %d", "", name, age, grade);

than to say this:

printf("               %s %d %d", name, age, grade);

At least in the first version, it is obvious how many leading spaces there are: 15.  In the second example, good luck counting the leading spaces.  Unless you write it with a comment something like this:

//                111111
printf( "               %s %d %d", name, age, grade);

but without a fixed-width font, it is still difficult to see, and even with a fixed-width font, it is still less readable than the first example using %15s instead of the string of spaces.

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question