www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - snprintf

↑ ↓ ← coder <coder_member pathlink.com> writes:
Does dmc implement the c99 function snprintf? My code containing calls to
snprintf won't link. However vsnprintf seems to be implemented which makes
snprintf easy to write. Is this an oversight.
May 10 2004
→ coder <coder_member pathlink.com> writes:
In article <c7ohe4$fma$1 digitaldaemon.com>, coder says...
Does dmc implement the c99 function snprintf? My code containing calls to
snprintf won't link. However vsnprintf seems to be implemented which makes
snprintf easy to write. Is this an oversight.

Also, I don't think vsnprintf is correct according to the standard. The program below always prints -1 when it should print 13. #include <stdio.h> #include <stdarg.h> int snprintf(char *str, int n, char *fmt, ...) { va_list a; va_start(a, fmt); int ret = vsnprintf(str, n, fmt, a); va_end(a); return ret; } int main() { char str[10]; int n = snprintf(str, 10, "Hello, World\n"); printf("%d\n",n); return 0; } The standard says: " The vsnprintf [also snprintf] function returns the number of characters that would have been written had n been sufficiently large, not counting the terminating null character, or a negative value if an encoding error occurred. Thus, the null-terminated output has been completely written if and only if the returned value is nonnegative and less than n. " Therefore the call to snprintf should return strlen("Hello World\n") so I'd know to allocate an array of at least strlen("Hello World\n")+1 bytes of memory. That allows code like this to work. int n = snprintf((char*)0, 0, fmt, a1, a2, aetc); char *str=malloc(n+1); snprintf(str, n+1, fmt, a1, a2, aetc); It solves the problem of not knowing how large sprintf's buffer should be.
May 10 2004
→ "Jackson" <fangzhengzhu kingsoft.com> writes:
If the number of charactors to written exceed count,then -1 will be
returned.

So just increase count.

"coder" <coder_member pathlink.com> 写入消息新闻
:c7ohe4$fma$1 digitaldaemon.com...
 Does dmc implement the c99 function snprintf? My code containing calls to
 snprintf won't link. However vsnprintf seems to be implemented which makes
 snprintf easy to write. Is this an oversight.

Aug 30 2004