?
Solved

Is there a space command in the print statement

Posted on 2002-07-24
5
Medium Priority
?
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

777 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