|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
hello,
is there a c++ equivalent function to the c-function 'sprintf'? Thank you, leo |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 2008-10-09 09:33:41 -0400, "A.Leopold" <andreas.leopold@himt.de> said:
> > is there a c++ equivalent function to the c-function 'sprintf'? > Yes. Its name is sprintf. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book) |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
![]() ok, I asked, cause my compiler (VS05) is complaining about the use of sprintf .... Pete Becker schrieb: > On 2008-10-09 09:33:41 -0400, "A.Leopold" <andreas.leopold@himt.de> said: > >> >> is there a c++ equivalent function to the c-function 'sprintf'? >> > > Yes. Its name is sprintf. > |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Oct 9, 3:33pm, "A.Leopold" <andreas.leop...@himt.de> wrote:
> hello, > > is there a c++ equivalent function to the c-function 'sprintf'? Maybe you want to use stringstream. > > Thank you, > > leo |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
juanvicfer schrieb: > On Oct 9, 3:33 pm, "A.Leopold" <andreas.leop...@himt.de> wrote: >> hello, >> >> is there a c++ equivalent function to the c-function 'sprintf'? > > Maybe you want to use stringstream. ok, I will have a look at stringstream |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Oct 9, 9:33 am, "A.Leopold" <andreas.leop...@himt.de> wrote:
> hello, > > is there a c++ equivalent function to the c-function 'sprintf'? You can also check out boost::format, which can be easier to work with than stringstream: http://www.boost.org/doc/libs/1_36_0...oc/format.html Sean |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
sean_in_raleigh@yahoo.com schrieb: > On Oct 9, 9:33 am, "A.Leopold" <andreas.leop...@himt.de> wrote: >> hello, >> >> is there a c++ equivalent function to the c-function 'sprintf'? > > > You can also check out boost::format, which can be > easier to work with than stringstream: > > http://www.boost.org/doc/libs/1_36_0...oc/format.html > > Sean thank you, that looks much more convenient! |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
"A.Leopold" <andreas.leopold@himt.de> writes:
> ![]() > ok, I asked, cause my compiler (VS05) is complaining about the use of > sprintf .... I guess it's rather ::sprintf (with extern "C" { / #include <stdio.h> / }). But if you want some safety, you'd use ::snprintf, and if you don't want to implement the safety yourself, you could rather use lisp^W std: stringstream.std: stringstream s; s<<"Hi "<<world<<std::endl;std::string str =s.str(); const char* cstr=s.c_str(); -- __Pascal Bourguignon__ |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
On Thu, 09 Oct 2008 17:12:56 +0200, A.Leopold wrote:
> sean_in_raleigh@yahoo.com schrieb: >> On Oct 9, 9:33 am, "A.Leopold" <andreas.leop...@himt.de> wrote: >>> hello, >>> >>> is there a c++ equivalent function to the c-function 'sprintf'? >> >> >> You can also check out boost::format, which can be easier to work with >> than stringstream: >> >> http://www.boost.org/doc/libs/1_36_0...oc/format.html >> >> Sean > > thank you, that looks much more convenient! There is also boost::lexical_cast (something like a "cheap and cheerful" version of stringstream formatting): http://www.boost.org/doc/libs/1_36_0...xical_cast.htm -- Lionel B |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
Pascal J. Bourguignon wrote:
> "A.Leopold" <andreas.leopold@himt.de> writes: > > > ![]() > > ok, I asked, cause my compiler (VS05) is complaining about the use > > of sprintf .... > > I guess it's rather ::sprintf (with extern "C" { / #include <stdio.h> > / }). What would make you guess something like that? Brian |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
"Default User" <defaultuserbr@yahoo.com> writes:
> Pascal J. Bourguignon wrote: > >> "A.Leopold" <andreas.leopold@himt.de> writes: >> >> > ![]() >> > ok, I asked, cause my compiler (VS05) is complaining about the use >> > of sprintf .... >> >> I guess it's rather ::sprintf (with extern "C" { / #include <stdio.h> >> / }). > > What would make you guess something like that? First, AFAIK, C functions are in the "root" namespace; to avoid refering a different object in the current namespace, it's advised (in various style guides) to qualify C functions. Then, using a function without declaring it would make a sane compiler complain, so it's good practice to include some header declaring it before using it. -- __Pascal Bourguignon__ |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
Pascal J. Bourguignon wrote:
> "Default User" <defaultuserbr@yahoo.com> writes: > >> Pascal J. Bourguignon wrote: >> >>> "A.Leopold" <andreas.leopold@himt.de> writes: >>> >>>> ![]() >>>> ok, I asked, cause my compiler (VS05) is complaining about the use >>>> of sprintf .... >>> I guess it's rather ::sprintf (with extern "C" { / #include <stdio.h> >>> / }). >> What would make you guess something like that? > > First, AFAIK, C functions are in the "root" namespace; to avoid > refering a different object in the current namespace, it's advised (in > various style guides) to qualify C functions. > > Then, using a function without declaring it would make a sane compiler > complain, so it's good practice to include some header declaring it > before using it. The is the C++ group here, so the C header <stdio.h> is to be included using <cstdio> and its content is accessible only within the 'std' namespace. Schobi |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
Pascal J. Bourguignon wrote:
> "Default User" <defaultuserbr@yahoo.com> writes: > > > Pascal J. Bourguignon wrote: > > > >> "A.Leopold" <andreas.leopold@himt.de> writes: > >> > >> > ![]() > >> > ok, I asked, cause my compiler (VS05) is complaining about the > use >> > of sprintf .... > >> > >> I guess it's rather ::sprintf (with extern "C" { / #include > <stdio.h> >> / }). > > > > What would make you guess something like that? > > First, AFAIK, C functions are in the "root" namespace; Not (at least theoretically) if you use the headers like <cstdio>. However, that's not necessary. > to avoid > refering a different object in the current namespace, it's advised (in > various style guides) to qualify C functions. I've never seen such a thing. > Then, using a function without declaring it would make a sane compiler > complain, so it's good practice to include some header declaring it > before using it. That's correct, of course. However, <stdio.h> is a standard C++ header. Why would you enclose it in extern "C"? Brian |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
Pete Becker wrote:
> On 2008-10-09 09:33:41 -0400, "A.Leopold" <andreas.leopold@himt.de> said: > >> >> is there a c++ equivalent function to the c-function 'sprintf'? >> > > Yes. Its name is sprintf. Assuming that line is preceded by "using std::sprintf;". |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
On Oct 9, 8:45am, "A.Leopold" <andreas.leop...@himt.de> wrote:
> ![]() > ok, I asked, cause my compiler (VS05) is complaining about the use of > sprintf .... Please don't top-post. If you're getting the MSVC warning about depreciated functions, you may ignore that, or turn off the warning with the appropriate compiler option, #pragma or #define. Current versions of MSVC warn about the use of many traditional functions which have the potential, if used incorrectly, to lead to buffer overflows. MS has taken a fair bit of heat for this, especially because they chose to describe the functions as "depreciated" (which means something specific in terms of the language standard), instead of something describing the risk. In most cases they provide a (Microsoft specific) replacement function which has the potential to be used more easily in a safe way (for example, the non-standard sprintf_s is offered as a "safer" replacement for sprintf). The did it to a number of C++ library functions to. For example, basic_string::copy is "depreciated" while the non-standard basic_string::_Copy_s is provided as a "safer" alternative. http://msdn.microsoft.com/en-us/libr...ys(VS.80).aspx |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
Jeff Schwab wrote:
> Pete Becker wrote: >> On 2008-10-09 09:33:41 -0400, "A.Leopold" <andreas.leopold@himt.de> said: >> >>> >>> is there a c++ equivalent function to the c-function 'sprintf'? >>> >> >> Yes. Its name is sprintf. > > Assuming that line is preceded by "using std::sprintf;". That depends on which header you include. From what I've seen, people still prefer <stdio.h> over <cstdio>. -- Ian Collins |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
A.Leopold wrote:
> is there a c++ equivalent function to the c-function 'sprintf'? Assuming that vnsprintf is C99 or C++0X compliant that std:string is contiguous (as in C++0X) and that writing \0 at str[ str.size() ] is harmless, you could try: bool strprintf ( std::string & buffer, char const * format, ... ) { while ( true ) { buffer.resize( buffer.capacity() ); int old_length = buffer.size(); std::va_list aq; va_start( aq, format ); int length_needed = vsnprintf( &buffer[0], old_length + 1, format, aq ); va_end( aq ); if ( length_needed < 0 ) { return ( false ); } buffer.resize( length_needed ); if ( length_needed <= old_length ) { return ( true ); } } } This will do what sprintf() does, except it will use a std::string as the target and increase it when needed. Best Kai-Uwe Bux |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
Ian Collins wrote:
> Jeff Schwab wrote: >> Pete Becker wrote: >>> On 2008-10-09 09:33:41 -0400, "A.Leopold" <andreas.leopold@himt.de> said: >>> >>>> is there a c++ equivalent function to the c-function 'sprintf'? >>>> >>> Yes. Its name is sprintf. >> Assuming that line is preceded by "using std::sprintf;". > > That depends on which header you include. From what I've seen, people > still prefer <stdio.h> over <cstdio>. Where are you seeing this? IME, it's vanishingly rare, and generally a sign of incompetence. |
|
|
|
#19 |
|
Messages: n/a
Hébergeur: |
Jeff Schwab wrote:
> Ian Collins wrote: >> Jeff Schwab wrote: >>> Pete Becker wrote: >>>> On 2008-10-09 09:33:41 -0400, "A.Leopold" <andreas.leopold@himt.de> said: >>>> >>>>> is there a c++ equivalent function to the c-function 'sprintf'? >>>>> >>>> Yes. Its name is sprintf. >>> Assuming that line is preceded by "using std::sprintf;". >> That depends on which header you include. From what I've seen, people >> still prefer <stdio.h> over <cstdio>. > > Where are you seeing this? IME, it's vanishingly rare, and generally a > sign of incompetence. I wish it were rare. Schobi |
|
|
|
#20 |
|
Messages: n/a
Hébergeur: |
In article <0494ee48-d50e-4ec3-8099-48c699c3dc2e@m32g2000hsf.googlegroups.com>,
robertwessel2@yahoo.com <robertwessel2@yahoo.com> wrote: >On Oct 9, 8:45am, "A.Leopold" <andreas.leop...@himt.de> wrote: >> ![]() >> ok, I asked, cause my compiler (VS05) is complaining about the use of >> sprintf .... > > >Please don't top-post. > >If you're getting the MSVC warning about depreciated functions, you >may ignore that, or turn off the warning with the appropriate compiler >option, #pragma or #define. Current versions of MSVC warn about the >use of many traditional functions which have the potential, if used >incorrectly, to lead to buffer overflows. MS has taken a fair bit of >heat for this, especially because they chose to describe the functions >as "depreciated" (which means something specific in terms of the >language standard), instead of something describing the risk. In most >cases they provide a (Microsoft specific) replacement function which >has the potential to be used more easily in a safe way (for example, >the non-standard sprintf_s is offered as a "safer" replacement for >sprintf). > >The did it to a number of C++ library functions to. For example, >basic_string::copy is "depreciated" while the non-standard >basic_string::_Copy_s is provided as a "safer" alternative. > >http://msdn.microsoft.com/en-us/libr...ys(VS.80).aspx I am nout sure you need to go that far into dodgy non-standard functions. sprintf should in almost all places be replaced by snprintf. snprintf is perfectly standard and I think it would silence the warning. Yannick |
|
|
|
#21 |
|
Messages: n/a
Hébergeur: |
In article <48ee0ae3$1@news.arcor-ip.de>,
A.Leopold <andreas.leopold@himt.de> wrote: > ![]() >ok, I asked, cause my compiler (VS05) is complaining about the use of >sprintf .... sprintf is a buffer overflow waiting to happen. Use snprintf instead. # man sprintf -------selected text-------- BUGS Because sprintf() and vsprintf() assume an arbitrarily long string, callers must be careful not to overflow the actual space; this is often impossible to assure. Note that the length of the strings produced is locale-dependent and difficult to predict. Use snprintf() and vsnprintf() instead (or asprintf() and vasprintf). ---------------------------- Alternatively, learn abount C++ streams but that would require more significant changes to your code. Yannick |
|
|
|
#22 |
|
Messages: n/a
Hébergeur: |
In message <1223980571.309748@irys.nyx.net>, Yannick Tremblay
<ytrembla@nyx.nyx.net> writes >In article <48ee0ae3$1@news.arcor-ip.de>, >A.Leopold <andreas.leopold@himt.de> wrote: >> ![]() >>ok, I asked, cause my compiler (VS05) is complaining about the use of >>sprintf .... > > >sprintf is a buffer overflow waiting to happen. > >Use snprintf instead. .... that way you only have an out-by-1 error waiting to happen ;-/ -- Richard Herring |
|
|
|
#23 |
|
Messages: n/a
Hébergeur: |
In article <vinvNJK$3H9IFwkq@baesystems.com>,
Richard Herring <richard.herring@baesystems.com> wrote: >In message <1223980571.309748@irys.nyx.net>, Yannick Tremblay ><ytrembla@nyx.nyx.net> writes >>In article <48ee0ae3$1@news.arcor-ip.de>, >>A.Leopold <andreas.leopold@himt.de> wrote: >>> ![]() >>>ok, I asked, cause my compiler (VS05) is complaining about the use of >>>sprintf .... >> >> >>sprintf is a buffer overflow waiting to happen. >> >>Use snprintf instead. > >... that way you only have an out-by-1 error waiting to happen ;-/ I did consider adding a warning of snprintf too since I have often encountered the out-by-one error. Not that long ago mentionning in passing to a programmer the he should use streams instead cause they are less bug prone but he replied that it was alright in that case cause he was safely using the same definition for his array size and his snprintf only for a couple of weeks later figuring out that there was indead a out-by-1 error in his code. The typical excuse of course is that C++ streams are not efficient (snprintf is faster) even if there has been no measurement done and that particular piece of code is not in a critical loop :-( But despite all of that, sprintf is fundamentally flawed in many situation because it can be impossible to write safe code with it as you can't verify that the buffer was big enough. snprintf makes it possible to write safe code. Even if you need to be careful with the out-by-1 errors. Personally, I am lazy. I use string streams :-) Yannick |
|
|
|
#24 |
|
Messages: n/a
Hébergeur: |
In article <0494ee48-d50e-4ec3-8099-48c699c3dc2e@m32g2000hsf.googlegroups.com>,
robertwessel2@yahoo.com <robertwessel2@yahoo.com> wrote: > >If you're getting the MSVC warning about depreciated functions, you >may ignore that, or turn off the warning with the appropriate compiler >option, #pragma or #define. Current versions of MSVC warn about the >use of many traditional functions which have the potential, if used >incorrectly, to lead to buffer overflows. MS has taken a fair bit of >heat for this, especially because they chose to describe the functions >as "depreciated" (which means something specific in terms of the >language standard), instead of something describing the risk. In most >cases they provide a (Microsoft specific) replacement function which >has the potential to be used more easily in a safe way (for example, >the non-standard sprintf_s is offered as a "safer" replacement for >sprintf). > >The did it to a number of C++ library functions to. For example, >basic_string::copy is "depreciated" while the non-standard >basic_string::_Copy_s is provided as a "safer" alternative. > >http://msdn.microsoft.com/en-us/libr...ys(VS.80).aspx > Arrghh!!! I went and glance at this link. what a bunch of idiots: -------------------------------------- size_type copy( value_type* _Ptr, size_type _Count, size_type _Off = 0 ) const; This method is potentially unsafe, as it relies on the caller to check that the passed values are correct. Consider using basic_string::_Copy_s instead. size_type _Copy_s( value_type *_Dest, size_type _Dest_size, size_type _Count, size_type _Off = 0 ) const; -------------------------------------------- Hmm, It also relies on the caller to send the correct value in _Dest_size. If I am too incompetent to send a valid _Count, how do they expect me to send a valid _Dest_size? Even worse for read: ------------------------------------------- basic_istream& read( char_type *_Str, streamsize _Count ); This method is potentially unsafe, as it relies on the caller to check that the passed values are correct. Consider using basic_istream::_Read_s instead. basic_istream& _Read_s( char_type *_Str, size_t _Str_size, streamsize _Count ); ------------------------------- So this is only ever useful if I am idiot enough to call this method with _Count > _Str_size. By all mean, deprecate functions like sprintf that have no way toindicate a maximum size. However, all they have done is duplicate an already exisiting safety mechanism. By all mean, it might, just might catch a couple of errors but mostly poor code will look like: #define MAX_SIZE 20; char c[MAX_SIZE]; cin._Read_s(&c[0], MAX_SIZE, MAX_SIZE); which has the same bug as: #define MAX_SIZE 20; char c[MAX_SIZE]; cin.read(&c[0], MAX_SIZE); // or cin.read(c, sizeof(c)) Bunch of .... Yannick |
|
|
|
#25 |
|
Messages: n/a
Hébergeur: |
On Oct 11, 5:03pm, Jeff Schwab <j...@schwabcenter.com> wrote:
> Ian Collins wrote: > > Jeff Schwab wrote: > >> Pete Becker wrote: > >>> On 2008-10-09 09:33:41 -0400, "A.Leopold" > >>> <andreas.leop...@himt.de> said: > >>>> is there a c++ equivalent function to the c-function 'sprintf'? > >>> Yes. Its name is sprintf. > >> Assuming that line is preceded by "using std::sprintf;". > > That depends on which header you include. From what I've seen, people > > still prefer <stdio.h> over <cstdio>. > Where are you seeing this? Probably in shops which prefer libraries which work as specified. > IME, it's vanishingly rare, and generally a sign of > incompetence. Using a library which works as specified is incompetent? Using one that doesn't isn't? Implementations where the <c...> libraries work as specified are so rare that the standards committee changed the specification. -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
|
![]() |
| Outils de la discussion | |
|
|