Sign in to follow this  
comfortablynumb84

strcpy_s??

Recommended Posts

A little question. I'm reading a book called "Beginning Visual C++ 2005" by Wiley. In one of its chapters, there's a function called strcpy_s().. and the book says that we must include the cstring header to use it. I include that header but it doesn't find any function called strcpy_s(). Is it called in that way or there's another one? It says that is a safer version of strcpy(). I need that function to copy an array of characters in another one, passing its size too. Thanks in advance.

Share this post


Link to post
Share on other sites
Antheus    2409
strcpy_s is a security enhanced strcpy.

If you truly want to improve code, use std::string for string manipulation, and stdext::checked_copy and related for more low level code.

Other that that, depending on how much experience you do have with software development in general, such issues might be completely irrelevant for you.

Share this post


Link to post
Share on other sites
Quote:

Are you doing:

#include <cstring>

or

#include <cstring.h>

?

According to MSDN it is found in string.h also.

Dave


I tried with <cstring>, with <cstring.h> and then with <string> and it doesn't find strcpy_s(). What's going on then?

Quote:

strcpy_s is a security enhanced strcpy.

If you truly want to improve code, use std::string for string manipulation, and stdext::checked_copy and related for more low level code.

Other that that, depending on how much experience you do have with software development in general, such issues might be completely irrelevant for you.


I'm trying some examples from my book. It uses strcpy_s() but it doesn't seems to be in <cstring>. BTW.. I'm using DevC++ 4.9.9.2. Maybe it's not up-to-date or something like that?

Share this post


Link to post
Share on other sites
ZQJ    496
Ah - strcpy_s() is MS specific. Dev-C++ uses the GNU C library (I think), so you don't have it. strncpy() is a similar (but not identical - it is absolutely not a drop in replacement) function which you probably do have. In any case in C++ there's rarely a reason to use these - stick to std::string.

Edit: incidentally, I'd also advise you ditch Dev-C++. Visual C++ Express is available free from MS (here), or if you don't want that for some reason, I gather Code::Blocks is also better than Dev-C++.

Share this post


Link to post
Share on other sites
Quote:

I may be wrong but isnt the *_s routines a Microsoft extension?
ie, not part of ANSI C++?


Yes, now it has sence.. I'll have to download VC++EE.

Quote:

Ah - strcpy_s() is MS specific. Dev-C++ uses the GNU C library (I think), so you don't have it. strncpy() is a similar (but not identical - it is absolutely not a drop in replacement) function which you probably do have. In any case in C++ there's rarely a reason to use these - stick to std::string.

Edit: incidentally, I'd also advise you ditch Dev-C++. Visual C++ Express is available free from MS (here), or if you don't want that for some reason, I gather Code::Blocks is also better than Dev-C++.


I had Code::Blocks once but DevC++ is so easy to use and I get comfortable with it. Sadly.. is not updated, so I'll have to move on another compiler anyway..

Thank you a lot!

Share this post


Link to post
Share on other sites
Spoonbender    1258
So, why is a book that claims to teach C++ (and is relatively recent, even) trying to get you to use obsolete C functions, secure MS extensions or not?

Just use std::string [grin]

Share this post


Link to post
Share on other sites
Antheus    2409
Quote:
Original post by Spoonbender
So, why is a book that claims to teach C++ (and is relatively recent, even) trying to get you to use obsolete C functions, secure MS extensions or not?

Just use std::string [grin]


Because it doesn't teach C++, or, as the title states: "Beginning Visual C++ 2005"?

While MVC++ 2005 is quite C++ standards compliant, it's an IDE, not language.

Quote:
BTW.. I'm using DevC++ 4.9.9.2. Maybe it's not up-to-date or something like that?


And yes, this is wrong. You can only use MVC++ 2005 with such book, standards or not. You're learning an IDE and platform, not language.

In beginning, it doesn't really matter, but looking at general C++ *language* books is a good idea.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this