473,805 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1419
Greetings,

jc**********@gm ail.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**********@g mail.com" <jc**********@g mail.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**********@gm ail.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************ **********@g44g 2000cwa.googleg roups.com>
jc**********@gm ail.com <jc**********@g mail.com> wrote:
fprintf( stderr, "%*s %s: \n", 6*level, "Target Entity ID: ",
get_entity_i d( 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]", "12345678901234 5678");
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
2475
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: aStream.width(20); aStream.precision(15); but then if there's a very small value, scientific notation is not used automatically and precision is lost (like in 0.000000000000003 instead of 3.33333333333e-15).
2
9529
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 ? #include <stdio.h> //part09_le01_file_processing_file_setup_ver_01_iti_r01_ch09.c struct name { int int___member1; float float_member2; char char__member3;
3
3165
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 and exits. I'd like to change the above to something like: fatal_error("somefunc(): %s has illegal character
4
1511
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 point values have a period, even when they are round numbers. This is a somewhat similar behavior to the %g flag of
17
2810
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 j = 0; j < 4; ++j) Why did you declare j as type size_t ?
2
2023
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 *'" Can someone help me?
4
2717
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 second always fails. Likewise any other example.
11
4309
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 C89 fwrite/fread were added to the C standard to allow portable binary IO to files. I wonder though why the choice was made to extend the unix function write() into a standard C function rather than to extend the existing standard C function...
3
3802
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 occuring at fprintf. code snippet : ********************************************************************************* #include<pthread.h>
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10614
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10369
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10109
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.