473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

fprintf formatting question???

I tryin' to print out a string with a variable number of blanks/spaces
preceding the string. I know that if I use:

fprintf( stdout, "%12s", string );

I get 12 blanks preceding 'string'. If I use:

n = 12;
fprintf( stdout, "%(n)s: ", string );

I get: %(n)s:

Any ideas?

---John

Nov 14 '05 #1
7 1400
Greetings,

jc**********@gmail.com wrote:
I tryin' to print out a string with a variable number of blanks/spaces
preceding the string. I know that if I use:

fprintf( stdout, "%12s", string );

I get 12 blanks preceding 'string'. If I use:

n = 12;
fprintf( stdout, "%(n)s: ", string );


fprintf( stdout, "%*s: ", n, string);

--
Kyle A. York
Sr. Subordinate Grunt
Nov 14 '05 #2
"jc**********@gmail.com" <jc**********@gmail.com> writes:
I tryin' to print out a string with a variable number of blanks/spaces
preceding the string. I know that if I use:

fprintf( stdout, "%12s", string );

I get 12 blanks preceding 'string'. If I use:

n = 12;
fprintf( stdout, "%(n)s: ", string );


printf("%*s: ", n, string);
--
"When in doubt, treat ``feature'' as a pejorative.
(Think of a hundred-bladed Swiss army knife.)"
--Kernighan and Plauger, _Software Tools_
Nov 14 '05 #3
Thanks much! ---John

Nov 14 '05 #4
On Fri, 20 May 2005 12:45:42 -0700, kyle york wrote:
Greetings,

jc**********@gmail.com wrote:
I tryin' to print out a string with a variable number of blanks/spaces
preceding the string. I know that if I use:

fprintf( stdout, "%12s", string );

I get 12 blanks preceding 'string'. If I use:
No, you will get leading blanks added to make the total length of the
field up to 12. You would only get 12 blanks added if string has zero
length.

n = 12;
fprintf( stdout, "%(n)s: ", string );


fprintf( stdout, "%*s: ", n, string);


Note that the argument passed for * muts have type int. Cast if n has some
other type.

Lawrence

Nov 14 '05 #5
Strange thing happened on the way to the forum:

fprintf( stderr, "%*s %s: \n", 6*level, "Target Entity ID: ",
get_entity_id( child, level+1 ) );

produces no leading blanks (spaces),

while:

fprintf( stderr, "%*s %s name: %s: \n", 6*level, "Node: ", child->name,
attr );

produces the expected/require leading blanks (spaces).

I checked the value of 'level' in the first fprintf and it was what I
expected (3). One other observation: if I replace the first fprintf
statement with the second (in the same routine) - it works as expects:
it adds the leading spaces.

Any thoughts?

Nov 14 '05 #6
In article <11**********************@g44g2000cwa.googlegroups .com>
jc**********@gmail.com <jc**********@gmail.com> wrote:
fprintf( stderr, "%*s %s: \n", 6*level, "Target Entity ID: ",
get_entity_id( child, level+1 ) );
Note: strlen("Target Entity ID: ") is 18.
produces no leading blanks (spaces), while:

fprintf( stderr, "%*s %s name: %s: \n", 6*level, "Node: ", child->name,
attr );

produces the expected/require leading blanks (spaces).
Note: strlen("Node: ") is 6.

Next, we have:
I checked the value of 'level' in the first fprintf and it was what I
expected (3).


Assuming "level" has type int (or promotes to type int), 6*level will
be 6*3, i.e., the "int" value 18.

printf("[%18s]", "123456789012345678");
printf("[%18s]", "123456");

should cause the square brackets to line up, because the first
string is 18 characters long and printed in an 18 character field,
while the second string is 6 characters long and printed in an 18
character field. The field width tells printf() to pad (with
spaces, in this case) as needed to make sure the output is at least
that long. Since 18 minus 18 is zero, the first printf() requires
no extra padding characters.

(If "level" has type long, or double, or similar, all bets are off,
because "%*s" needs one (int) and one (char *). I suspect this is
not a problem, though.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #7
Got it, thanks! ---John

Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Koen | last post by:
Hi, could anyone please tell me how I can get the equivalent of this: fprintf(aFile,"-20.15g",aDouble); but in C++ using formatted streams? I tried with C++ formatted streams using:...
2
by: hpy_awad | last post by:
formatting float variables to fprintf has error to my writing to output file called rental and I do not the reason that a rabish is written to the file instead of the actual input screen values ? ...
3
by: Andrew Fabbro | last post by:
I have code with stuff like this all over it: sprintf(errmsg,"somefunc(): %s has illegal character %c",somestring,somechar); fatal_error(errmsg); where fatal_error() just fprintf's to stderr...
4
by: GGarramuno | last post by:
I have a program that expects its input in a specific format. Mainly, it expects floating-point values to be formatted in the form: 1.32 1. 3. 3.2345 In case you missed it, all floating...
17
by: G Patel | last post by:
E. Robert Tisdale wrote: > > int main(int argc, char* argv) { > quad_t m = {0, 1, 2, 3}; > int r; > fprintf(stdout, "m = ("); > for (size_t...
2
by: Joy2006 | last post by:
I am a beginner of VC++... Whenever I use fprintf(fp,Count); where Count is a Long Variable, I get an error saying "error C2664: 'fprintf' : cannot convert parameter 2 from 'long' to 'const char...
4
by: grimrob | last post by:
fprintf just is not working. I am using PHP Version 4.3.9. Whatever I do fprintf just fails. For example: fwrite($Handle, 'A1'); fprintf($Handle, 'A2); The first line always works, the...
11
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At...
3
by: vamsi | last post by:
Hi, I have a program, where involves creation of a thread with stack size of 16k(minimum stack size). There is an fprintf statement in the created thread code. I see that there is a core dump...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.