导致分段故障c和x2B的Strncpy+; 我有一个C++程序,它读取文本文件,然后将文本文件转换为字符串。然后,它使用strncpy将字符串转换为字符数组。我已经在strncpy上看到了stackoverflow问题,并采取了必要的预防措施,以避免在创建阵列时它所导致的问题。有人能解释一下为什么它仍然会导致堆栈错误吗 #include <iostream> #include <string.h> #include <random> #include <fstream> #include <istream> #include <sstream> #include <stdio.h> using namespace std; int main() { //open a stream reader ifstream fin; //opens the text file in the stream reader fin.open("songlyrics.txt"); //will be used to aggregate all characters in text file string song; //used as a pointer when reading each character in text file char ch; //while the end of file is not reached while(!fin.eof()) { //get the character from the file and add it to the song string fin.get(ch); song += ch; } //close the file fin.close(); //make a character array called lyrics_ with a length of the length of song char lyrics_[song.length()]; //use strncpy to convert song to a char array, lyrics_ strncpy(lyrics_, song.c_str(), sizeof(lyrics_)); //avoid the segmentation fault lyrics_[sizeof(lyrics_) - 1] = 0; cout<<lyrics_; return 0; } #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main() { //打开流阅读器 流鳍; //在流读取器中打开文本文件 fin.open(“songlyps.txt”); //将用于聚合文本文件中的所有字符 弦乐曲; //在读取文本文件中的每个字符时用作指针 char ch; //而未到达文件的结尾 而(!fin.eof()) { //从文件中获取字符并将其添加到歌曲字符串中 fin.get(ch); 宋+=ch; } //关闭文件 fin.close(); //制作一个名为“歌词”的字符数组,长度为歌曲的长度 char歌词[song.length()]; //使用strncpy将歌曲转换为字符数组、歌词_ strncpy(歌词,song.c_str(),sizeof(歌词)); //避免分割错误 歌词[sizeof(歌词)-1]=0; 不能

导致分段故障c和x2B的Strncpy+; 我有一个C++程序,它读取文本文件,然后将文本文件转换为字符串。然后,它使用strncpy将字符串转换为字符数组。我已经在strncpy上看到了stackoverflow问题,并采取了必要的预防措施,以避免在创建阵列时它所导致的问题。有人能解释一下为什么它仍然会导致堆栈错误吗 #include <iostream> #include <string.h> #include <random> #include <fstream> #include <istream> #include <sstream> #include <stdio.h> using namespace std; int main() { //open a stream reader ifstream fin; //opens the text file in the stream reader fin.open("songlyrics.txt"); //will be used to aggregate all characters in text file string song; //used as a pointer when reading each character in text file char ch; //while the end of file is not reached while(!fin.eof()) { //get the character from the file and add it to the song string fin.get(ch); song += ch; } //close the file fin.close(); //make a character array called lyrics_ with a length of the length of song char lyrics_[song.length()]; //use strncpy to convert song to a char array, lyrics_ strncpy(lyrics_, song.c_str(), sizeof(lyrics_)); //avoid the segmentation fault lyrics_[sizeof(lyrics_) - 1] = 0; cout<<lyrics_; return 0; } #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main() { //打开流阅读器 流鳍; //在流读取器中打开文本文件 fin.open(“songlyps.txt”); //将用于聚合文本文件中的所有字符 弦乐曲; //在读取文本文件中的每个字符时用作指针 char ch; //而未到达文件的结尾 而(!fin.eof()) { //从文件中获取字符并将其添加到歌曲字符串中 fin.get(ch); 宋+=ch; } //关闭文件 fin.close(); //制作一个名为“歌词”的字符数组,长度为歌曲的长度 char歌词[song.length()]; //使用strncpy将歌曲转换为字符数组、歌词_ strncpy(歌词,song.c_str(),sizeof(歌词)); //避免分割错误 歌词[sizeof(歌词)-1]=0; 不能,c++,string,arrays,strncpy,C++,String,Arrays,Strncpy,这是: 是一个可变长度数组,不是标准C++。 另外,您不需要将std::string转换为字符数组 char* lyrics = &song[0]; // assuming you don't append to song in the future 如果确实需要单独的字符缓冲区,则必须动态分配: char* lyrics = new char[song.length() + 1]; memcpy(lyrics, song.c_str(), song.length() + 1); //

这是:

是一个可变长度数组,不是标准C++。 另外,您不需要将

std::string
转换为字符数组

char* lyrics = &song[0]; // assuming you don't append to song in the future
如果确实需要单独的字符缓冲区,则必须动态分配:

char* lyrics = new char[song.length() + 1];
memcpy(lyrics, song.c_str(), song.length() + 1); // this will copy the null terminator
delete [] lyrics; // don't forget this
这:

是一个可变长度数组,不是标准C++。 另外,您不需要将

std::string
转换为字符数组

char* lyrics = &song[0]; // assuming you don't append to song in the future
如果确实需要单独的字符缓冲区,则必须动态分配:

char* lyrics = new char[song.length() + 1];
memcpy(lyrics, song.c_str(), song.length() + 1); // this will copy the null terminator
delete [] lyrics; // don't forget this

C++不支持C中的可变长度数组功能。VLA是C.1999的标准功能,也是C.2011的可选功能

如果要将字符串内容复制到动态大小的
char
数组中,可以使用
向量

std::vector<char> lyrics_(song.begin(), song.end());
lyrics_.push_back('\0');
std::cout << &lyrics_[0];
std::vector歌词(song.begin(),song.end());
歌词u.push_back('\0');

std::coutC++不支持C中的可变长度数组功能。VLA是C.1999的标准功能,也是C.2011中的可选功能

如果要将字符串内容复制到动态大小的
char
数组中,可以使用
向量

std::vector<char> lyrics_(song.begin(), song.end());
lyrics_.push_back('\0');
std::cout << &lyrics_[0];
std::vector歌词(song.begin(),song.end());
歌词u.push_back('\0');

std::cout还值得注意的是,
eof()
在读取完文件末尾之后才会返回true。您应该处理
fin.get
的故障。还值得注意的是,
eof()
在读取完文件末尾后才会返回true。您应该处理
fin.get
@yzt的故障-停止编辑,我故意将其设置为
char*
。@Barry:您编写的代码是错误的。在标准中不能保证
std::string
是一个连续的字符块字符。@yzt在C++11中有,在新的更新之前编译器已经用这种方式实现了它。@Barry Oh我明白了。谢谢,但这真的很奇怪,因为当我编译代码的最初几次时,我没有segfault,然后它突然发生了。但是现在,当我为一个单独的缓冲区运行代码,并将其输出到控制台时,我忘记了“我可以得到整个数组,但我没有输出。”巴里,我也完全不理解字符串和字符类型的C和C++用法之间的区别。现在我的代码简单得多,我只是使用字符串作为数组而不是试图创建一个新数组。就像你说的,我不需要把字符串转换成字符数组。。现在一切都变得更有意义了,谢谢。@yzt-停止编辑,我故意把它做成
char*
。@Barry:你写的代码是错误的。标准中不能保证
std::string
是一个连续的字符块。@yzt在C++11中有,编译器在新的更新。@Barry Oh我知道了。谢谢,但这真的很奇怪,因为当我编译代码的前几次我没有得到segfault,然后它突然开始发生。但是现在当我为一个单独的缓冲区运行代码并将其输出到控制台时,我应该得到整个数组,但我没有得到任何输出。@Barry我也完全不明白到现在为止,C和C++的用法不同,我的代码现在简单多了,我只是使用字符串作为数组,而不是尝试创建一个新数组。就像你说的,我不需要把字符串转换成字符数组。现在,一切都变得更有意义了。