Solved

Is there a space command in the print statement

Posted on 2002-07-24
5
467 Views
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);
or
CString text;
text.Format("               %s  %d  %d\n", name, age, grade);
0
Comment
Question by:adatd
5 Comments
 
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;
               

     }

     SpacesFile.close();
 
  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())
{
TenforeFeedResults<<setw(10)<<"Forex"<<setw(6)<<"1"<<endl;
0
 
LVL 1

Accepted Solution

by:
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);
    cout<<sLine<<endl;

    //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);
    cout<<sLine<<endl;
} //end WriteLine()


void Pause()
{
    cout<<endl<<"Press Enter to continue...";
    char Enter;
    cin.get(Enter);
}

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

    Pause();
}
0
 

Expert Comment

by:Hursev
ID: 7174733
Hi adatd,
try this:

int iSpaces = 5;
const char* pszEmptyString = "";
printf("%*s",iSpaces,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);
0
 
LVL 30

Expert Comment

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

Example:

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

Expert Comment

by:ris
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
//spaces:123456789012345
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.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now