gpt4 book ai didi

c++ - SIGABRT on std::ifstream关闭

转载 作者:行者123 更新时间:2023-12-03 07:15:49 25 4
gpt4 key购买 nike

1我目前正在开发一个在OpenGL中创建自己的游戏的项目。我现在的问题是,如果我读取文件,则读取该文件的函数将导致SIGABRT,这是因为std::ifstream解构函数(具体而言是“std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()”)中的内容。该功能以前对我有用,但是突然停止了工作。
我的目标很简单:将文件读取为char *的可靠实现。目前我不关心多线程。
这是我对文件读取功能的实现。
它采用一个路径,并将该路径上的文件内容写入out参数。

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <cassert>
#include "Utility.h"
char * Utility::readFile(const char* path,char*& out){
#ifndef NDEBUG
std::cout<<"Getting file: "<<path<<"\n";
#endif
// Open the file, but freak out if not valid.
std::ifstream file=std::ifstream(path);
assert(file.good());
if(!file.good())
{
throw std::runtime_error((std::string)"Couldn't open file for loading: "+path);
}

// Read the file contents into a char buffer.
std::stringstream buffer;buffer << file.rdbuf();
std::string fileContentsStr = buffer.str();
out = new char[fileContentsStr.size()];
strcpy(out,fileContentsStr.c_str());
return out;
}
我的代码位于 C0D3-M4513R/OpenGlGame
我已经尝试了一个最小的示例,该示例正在运行并使用相同的编译标志(链接器标志除外)。 test.txt和test1.txt只是包含一些乱码,这些乱码是由我的键盘上的随机黑客产生的。
#include <cassert>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>

//This Function is the same as the one above!!!
char *readFile(const char *path, char *&out) {
#ifndef NDEBUG
std::cout << "Getting file: " << path << "\n";
#endif
// Open the file, but freak out if not valid.
std::ifstream file = std::ifstream(path);
assert(file.good());
if (!file.good()) {
throw std::runtime_error((std::string) "Couldn't open file for loading: " + path);
}

// Read the file contents into a char buffer.
std::stringstream buffer;
buffer << file.rdbuf();
//convert the stringstream to a string
std::string fileContentsStr = buffer.str();
//copy the contents of the string to a char array
out = new char[fileContentsStr.size()];
strcpy(out, fileContentsStr.c_str());
//return char array address (which should be the same as the start?)
return out;
}

int main() {
//The programm started!
std::cout << "Hello, World!" << std::endl;
//Define a space for the contents of the file to live
char *out;
//Read the contents of a file
out = readFile("test.txt", out);
//Print contents of the file
std::cout << out << std::endl;
char *out1;
//Read the contents of a file
out1 = readFile("test1.txt", out1);
//Print contents of the file
std::cout << out1 << std::endl;
return 0;
}

最佳答案

strcpy :

Copies the character string pointed to by src, including the null terminator, to the character array whose first element is pointed to by dest.The behavior is undefined if the dest array is not large enough. The behavior is undefined if the strings overlap.


c_str :

Returns a pointer to a null-terminated character array with data equivalent to those stored in the string.


out = new char[fileContentsStr.size()];
strcpy(out,fileContentsStr.c_str());

在将 std::string与c字符串混合时,您需要小心,因为 std::string并非以空字符结尾,并且不计算其终止符的大小。但是, c_str确实返回了一个以空字符结尾的字符数组的指针。
您正在要求 strcpyfileContentsStr.size()+1(大小+空终止符)写入仅包含 fileContentsStr.size()元素的char数组中。
PS:如评论中所述,您应该考虑返回 std::string。您使用的是原始拥有指针,该指针容易出错,应避免使用。要么使用智能指针,要么让 std::string管理char数组(这实际上是为它做的;)。

关于c++ - SIGABRT on std::ifstream关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63034922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com