What is the equivalent of strncpy
in C++? strncpy
works in C, but fails in C++.
This is the code I am attempting:
string str1 = "hello";
string str2;
strncpy (str2,str1,5);
The equivalent of C's strncpy()
(which, BTW, is spelled std::strncpy()
in C++, and is to found in the header <cstring>
) is std::string
's assignment operator:
std::string s1 = "Hello, world!";
std::string s2(s1); // copy-construction, equivalent to strcpy
std::string s3 = s1; // copy-construction, equivalent to strcpy, too
std::string s4(s1, 0, 5); // copy-construction, taking 5 chars from pos 0,
// equivalent to strncpy
std::string s5(s1.c_str(), std::min(5,s1.size()));
// copy-construction, equivalent to strncpy
s5 = s1; // assignment, equivalent to strcpy
s5.assign(s1, 5); // assignment, equivalent to strncpy
You can use basic_string::copy on a std::string
version of your const char*
, or use std::copy , which can use pointers as the input iterators.
What do you mean by "strncpy fails in C++", by the way?
strncpy
works on characters array, and it works just fine in C++ as well as in C.
If you are using strncpy
with characters array in C++, and it doesn't work, it's probably because of some error in your C++ code: show us if you want some help.
If what you are looking for is a way to copy strings (ie: std::string
), string's copy constructor or assignment operator will do what you're looking for:
std::string a = "hello!";
This will copy the whole string without any risk of buffer overflow. Anyway, as John Dibling says in the comments, it doesn't supply the same semantics of strncpy
: it doesn't let you specify how many characters to copy.
If you need to copy up to a certain number of characters there are other ways: std::string
offers a constructor that copier up to n
characters, or you could use the other ways proposed in other answers.
strncpy is available from the cstring header
in C++. Because it is C specific function with you have to deal with C kind of strings only, that is character arrays, not the string objects of C++
#include<cstring>
using namespace std;
int main(int argc, char *argv)
{
char *s,*v;
strncpy(s,v,0);
}
The above will compile properly, but will serve no useful purpose. When you are using C++, try to use the string
objects and its copy method of assignment operator to assign to a different string. Copy method takes a parameter where you can specify the number of characters to copy to the target string object.
strncpy does not take std::string
as arguments. The prototype is
char * strncpy ( char * destination, const char * source, size_t num );
and the provided link gives a description and example on how to use it.
I would stick with std::string
but if you have to, then use c_str()
std::string
method to obtain the char *
pointer
There is no built-in equivalent. You have to roll your own strncpy.
#include <cstring>
#include <string>
std::string strncpy(const char* str, const size_t n)
{
if (str == NULL || n == 0)
{
return std::string();
}
return std::string(str, std::min(std::strlen(str), n));
}
The equivalent of strncpy
in C++ is strncpy
.
You're just doing something wrong in your code, please post your code and the error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.