简体   繁体   English

将字符串转换为数组,strcpy不起作用

[英]Convert string to array, strcpy don't work

i tryed to use this algorithm to convert a string to an array. 我尝试使用此算法将字符串转换为数组。 the problem is this: strcpy don't work. 问题是这样的:strcpy不起作用。

i tried also: strcpy_s strncpy memcpy --> with this function my array can print only the 1st word ( dunno why )... 我也试过:strcpy_s strncpy memcpy - >用这个函数我的数组只能打印第一个单词(不知道为什么)...

string tmp;
getline(cin, tmp);
char* messaggio = new char[tmp.size()];
ZeroMemory(messaggio, tmp.size());
strcpy(messaggio, tmp.c_str());

tmp.resize(NULL);

i use Visual Studio 2013... when i try to use strcpy i have a strange error: C4996 may be unsafe. 我使用Visual Studio 2013 ...当我尝试使用strcpy我有一个奇怪的错误:C4996可能不安全。 if i try with strcpy_s is the same, same with strncpy... 如果我尝试使用strcpy_s是相同的,与strncpy相同...

One problem in your code, is that string::size gives you the number of characters excluding the null termination. 您的代码中的一个问题是string::size为您提供了除空终止之外的字符数。 So messagio does not have enough room for a nul-terminated string, and strcpy will try to write beyond its bounds. 因此, messagio没有足够的空间容纳一个以空字符结尾的字符串,而strcpy会尝试超出其范围。

As for the warning, it is because strcpy makes it very easy to go out of bounds. 至于警告,这是因为strcpy使它很容易走出界限。 If the length of the source string is greater than that of the destination buffer, you get undefined behaviour. 如果源字符串的长度大于目标缓冲区的长度,则会得到未定义的行为。 The suggested alternatives give you means to protect yourself from that (but are not 100% fool-proof). 建议的替代方案为您提供了保护自己的手段(但不是100%万无一失)。

If what you want is an array-like object containing the characters of the string, then the idiomatic way to do this is to use a vector: 如果你想要的是一个包含字符串字符的类数组对象,那么执行此操作的惯用方法是使用向量:

std::vector<char> messagio(tmp.begin(), tmp.end());

If you really want a character string, then just use std::string . 如果你真的想要一个字符串,那么只需使用std::string

The error you got in fact is not an error. 你实际上得到的错误不是错误。 It is a warning. 这是一个警告。 Maybe you set on the option of the compiler that forces the compiler to consider all warnings as errors. 也许您设置了编译器的选项,强制编译器将所有警告视为错误。

Your code has a bug. 您的代码有错误。 The size of the allocated array shall be one greater than the size of the string that to accomodate the terminating zero used by function std::strcpy 分配的数组的大小应该大于字符串的大小,以容纳函数std::strcpy使用的终止零

So you should write 所以你应该写

std::string tmp;
std::getline( std::cin, tmp );

char* messaggio = new char[tmp.size() + 1];
std::strcpy( messaggio, tmp.c_str() );

Or you could write 或者你可以写

strcpy_s( messaggio, tmp.size() + 1, tmp.c_str() );

though function strcpy_s is not a standard C++ function. 虽然函数strcpy_s不是标准的C ++函数。 It is present only in the C Standard. 它仅存在于C标准中。

I am sure you should ignore this warning. 我相信你应该忽略这个警告。

If your compiler supports class std::dynarray you could use it instead of the raw pointer. 如果您的编译器支持类std::dynarray您可以使用它而不是原始指针。

For example 例如

std::dynarray<char> messagio( tmp.size() + 1 );
std::strcpy( messagio.data(), tmp.c_str() ); 

[SOLVED] [解决了]

thx at all, expecially to "Vlad from Moscow" :3 总而言之,特别是“来自莫斯科的弗拉德”:3

std::string tmp;
        tmp.resize(0);
        std::getline(std::cin, tmp);
        char* messaggio = new char[tmp.size()+1];

        strcpy_s(messaggio, tmp.size()+1, tmp.c_str());

        send(ConnectSocket, messaggio, tmp.size()+1, NULL); //Send 2

        tmp.resize(0);
        delete messaggio;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM