473,414 Members | 1,936 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,414 software developers and data experts.

help: %*s in sprintf

hi, all:

I had a question about the formatted string while using sprintf(), can %*s truncate a string if the size of it exceeds? for example:
sprintf(zcMessageText, "%ld : %*s", nMyErrMsgKey,SYSMSG_SIZE-25, zcMessage);
if the size of zcMessage exceeds SYSMSG_SIZE, can we use sprintf with %*s to make buffer not overflow?
In my understanding, it only formats string if its size is less than or equal to the width specified, otherwise, just try to put everything in.
Please give me some advice on that, thanks.
Jun 1 '07 #1
2 1690
gpraghuram
1,275 Expert 1GB
hi,
If you want to limit the size of string being printed to the buffer then you should use snprintf

Thanks
Raghuram
Jun 1 '07 #2
AdrianH
1,251 Expert 1GB
You method should work, so would snprintf().

Hmmm, I just tested %.*s on g++ 3.4.4 and it didn't work. As far as I know it should work. :(

The only problem with snprintf() is that I've found not all compilers have it (or have it hidden). I'm not sure how previlant this problem is though.


Adrian
Jun 1 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Francesco | last post by:
Ciao, I'm new in c++ programming and I've experimented some method to convert real number into c++ string object, at present I've found as best solution use the C command "sprintf":...
4
by: Sridhar | last post by:
HI All, Currently writing a small program and here is my xml file and program. --------------------------------------------------------------------------------- <?xml version="1.0"...
6
by: jt | last post by:
I need to produce 1 character array from 3 others. I tried sprintf and it terminates on the first 0, null, 0x00 it sees in tmp data. All 3 args print out nice by themselves. By trying to make...
1
by: jimjim | last post by:
Hello, I was wondering about the implications of giving as an argument to sprintf a different data type from the one specified in the format argument. This type of question along with some...
1
by: Dale Magnuson | last post by:
If anyone can help me it would be greatly appreciated I am struggling with connection to a DDE server - I have a global variable defined as my conversation handle (newDDE) and the instance defined...
2
by: Vitali Gontsharuk | last post by:
Hi! I have a problem programming a simple client-server game, which is called pingpong ;-) The final program will first be started as a server (nr. 2) and then as a client. The client then...
1
by: chadw | last post by:
Not sure what I am missing sure its probably something very simple, I need something to get my entered text out of the textbox and entered into a database currently the code looks like this. ...
15
by: krister | last post by:
Hello, I'm working in a quite large system that has some limitations. One of those is that I can't use printf() to get an output on a screen. I'm forced to use a special function, let's call it...
10
by: xiao | last post by:
Can anyone help me to find out what are the structure of 'DataTable' ? Does loop the 14 layers or the 800*800 array first? (NumX=NumY=800) gridde and WriteHeader are two structures Thank you~~~...
11
by: pgfdbug | last post by:
Below is the code for the program that runs my firehouses alerting system. It reads packets that come to our printer and gives us an audible alert on what piece is due. The two issuse I have with...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.