[英]C++ Equivalent of strncpy
What is the equivalent of strncpy
in C++? 什么是C ++中
strncpy
的等价物? strncpy
works in C, but fails in C++. strncpy
在C中工作,但在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: 相当于C的
strncpy()
(BTW,在C ++中拼写为std::strncpy()
,在头文件<cstring>
)是std::string
的赋值运算符:
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. 您可以在
const char*
的std::string
版本上使用basic_string :: copy ,或者使用std :: copy ,它可以使用指针作为输入迭代器。
What do you mean by "strncpy fails in C++", by the way? 顺便说一下,“strncpy在C ++中失败了”是什么意思?
strncpy
works on characters array, and it works just fine in C++ as well as in C. strncpy
适用于字符数组,它在C ++和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. 如果您在C ++中使用带有字符数组的
strncpy
,并且它不起作用,可能是因为您的C ++代码中出现了一些错误:如果您需要帮助,请告诉我们。
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
),字符串的复制构造函数或赋值运算符将执行您正在寻找的内容:
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. 无论如何,正如John Dibling在评论中所说,它不提供与
strncpy
相同的语义:它不允许您指定要复制的字符数。
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. 如果您需要复制一定数量的字符,还有其他方法:
std::string
提供最多n
字符复制的构造函数,或者您可以使用其他答案中提出的其他方法。
strncpy is available from the cstring header
in C++. strncpy可以从C ++的
cstring header
中获得。 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++ 因为它是C特定的函数,你必须只处理C类字符串,即字符数组,而不是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. 在使用C ++时,请尝试使用
string
对象及其赋值运算符的复制方法来指定其他字符串。 Copy method takes a parameter where you can specify the number of characters to copy to the target string object. Copy方法采用一个参数,您可以在其中指定要复制到目标字符串对象的字符数。
strncpy does not take std::string
as arguments. strncpy不会将
std::string
作为参数。 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 我会坚持使用
std::string
但如果必须,那么使用c_str()
std::string
方法获取char *
指针
There is no built-in equivalent. 没有内置的等价物。 You have to roll your own strncpy.
你必须滚动你自己的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
. 的等效
strncpy
在C ++是strncpy
。
You're just doing something wrong in your code, please post your code and the error. 您只是在代码中做错了,请发布您的代码和错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.