Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从二进制文件读取std::string_C++ - Fatal编程技术网

C++ 从二进制文件读取std::string

C++ 从二进制文件读取std::string,c++,C++,我刚才创建了几个函数,用于将std::strings读写到以二进制模式打开的文件*中。它们以前工作得很好(WriteString()仍然有效),但ReadString()在运行时不断给我带来内存损坏错误。通过将字符串的大小写为无符号int,然后将字符串数据写为char来存储字符串 bool WriteString(std::string t_str, FILE* t_fp) { // Does the file stream exist and is it valid? If not, retu

我刚才创建了几个函数,用于将std::strings读写到以二进制模式打开的文件*中。它们以前工作得很好(WriteString()仍然有效),但ReadString()在运行时不断给我带来内存损坏错误。通过将字符串的大小写为无符号int,然后将字符串数据写为char来存储字符串

bool WriteString(std::string t_str, FILE* t_fp) {
// Does the file stream exist and is it valid? If not, return false.
if (t_fp == NULL) return false;
// Create char pointer from string.
char* text = const_cast<char*>(t_str.c_str());
// Find the length of the string.
unsigned int size = t_str.size();
// Write the string's size to the file.
fwrite(&size, sizeof(unsigned int), 1, t_fp);
// Followed by the string itself.
fwrite(text, 1, size, t_fp);
// Everything worked, so return true.
return true;

}



std::string ReadString(FILE* t_fp) {
// Does the file stream exist and is it valid? If not, return false.
if (t_fp == NULL) return false;
// Create new string object to store the retrieved text and to return to the calling function.
std::string str;
// Create a char pointer for temporary storage.
char* text = new char;
// UInt for storing the string's size.
unsigned int size;
// Read the size of the string from the file and store it in size.
fread(&size, sizeof(unsigned int), 1, t_fp);
// Read [size] number of characters from the string and store them in text.
fread(text, 1, size, t_fp);
// Store the contents of text in str.
str = text;
// Resize str to match the size else we get extra cruft (line endings methinks).
str.resize(size);
// Finally, return the string to the calling function.
return str;

}
bool WriteString(std::string t_str,FILE*t_fp){
//文件流是否存在且有效?如果不存在,则返回false。
如果(t_fp==NULL)返回false;
//从字符串创建字符指针。
char*text=const_cast(t_str.c_str());
//查找字符串的长度。
无符号整数大小=t_str.size();
//将字符串的大小写入文件。
fwrite(&size,sizeof(unsigned int),1,t_fp);
//然后是字符串本身。
fwrite(文本,1,大小,t_fp);
//一切都成功了,所以回归现实吧。
返回true;
}
std::string ReadString(文件*t\u fp){
//文件流是否存在且有效?如果不存在,则返回false。
如果(t_fp==NULL)返回false;
//创建新字符串对象以存储检索到的文本并返回调用函数。
std::字符串str;
//为临时存储创建一个字符指针。
字符*文本=新字符;
//用于存储字符串大小的UInt。
无符号整数大小;
//从文件中读取字符串的大小,并将其存储为大小。
fread(&size,sizeof(unsigned int),1,t_fp);
//从字符串中读取[size]个字符并将其存储在文本中。
fread(文本,1,大小,t_fp);
//将文本内容存储在str中。
str=文本;
//调整str的大小以匹配大小,否则我们会得到额外的粗糙度(我认为行尾)。
str.resize(大小);
//最后,将字符串返回给调用函数。
返回str;
}
是否有人看到此代码存在任何问题或有其他建议?

问题是:

char* text = new char;
您正在分配单个字符。在知道
size
后进行分配,并分配所需的所有
size
字符(例如,使用
新字符[size]
)。(为了避免泄漏,当然要在复制后删除它)。

问题是:

char* text = new char;

您正在分配单个字符。在知道
size
后进行分配,并分配所需的所有
size
字符(例如,使用
新字符[size]
)。(为了避免泄漏,当然要在复制后删除它)。

我遇到的最大问题是:

// Create a char pointer for temporary storage.
char* text = new char;
// ...
// Read [size] number of characters from the string and store them in text.
fread(text, 1, size, t_fp);
这将创建文本作为指向单个字符的指针,然后尝试读取任意数量的字符(可能不止一个)。为了使其正常工作,您必须在确定大小后将文本创建为字符数组,如下所示:

// UInt for storing the string's size.
unsigned int size;
// Read the size of the string from the file and store it in size.
fread(&size, sizeof(unsigned int), 1, t_fp);
// Create a char pointer for temporary storage.
char* text = new char[size];
// Read [size] number of characters from the string and store them in text.
fread(text, 1, size, t_fp);
其次,您不会释放分配给文本的内存。您需要这样做:

// Free the temporary storage
delete[] text;

最后,你有没有选择C++中使用C文件I/O的好理由?使用C++风格的iostreams可以缓解所有这些问题,并使您的代码更简短、更可读。

我遇到的最大主要问题是:

// Create a char pointer for temporary storage.
char* text = new char;
// ...
// Read [size] number of characters from the string and store them in text.
fread(text, 1, size, t_fp);
这将创建文本作为指向单个字符的指针,然后尝试读取任意数量的字符(可能不止一个)。为了使其正常工作,您必须在确定大小后将文本创建为字符数组,如下所示:

// UInt for storing the string's size.
unsigned int size;
// Read the size of the string from the file and store it in size.
fread(&size, sizeof(unsigned int), 1, t_fp);
// Create a char pointer for temporary storage.
char* text = new char[size];
// Read [size] number of characters from the string and store them in text.
fread(text, 1, size, t_fp);
其次,您不会释放分配给文本的内存。您需要这样做:

// Free the temporary storage
delete[] text;

最后,你有没有选择C++中使用C文件I/O的好理由?使用C++风格的iostreams可以减轻所有这些问题,并使您的代码变得更加简短和可读。

很抱歉,所选答案对我不适用

// UInt for storing the string's size.
unsigned int size;
// Read the size of the string from the file and store it in size.
fread(&size, sizeof(unsigned int), 1, t_fp);
// Create a char pointer for temporary storage.
char* text = new char[size];
// Read [size] number of characters from the string and store them in text.
fread(text, 1, size, t_fp);

这个数字最终是一个非常大的数字。我错过什么了吗

很抱歉,选择的答案不适合我

// UInt for storing the string's size.
unsigned int size;
// Read the size of the string from the file and store it in size.
fread(&size, sizeof(unsigned int), 1, t_fp);
// Create a char pointer for temporary storage.
char* text = new char[size];
// Read [size] number of characters from the string and store them in text.
fread(text, 1, size, t_fp);

这个数字最终是一个非常大的数字。我错过什么了吗

谢谢你的回答。我使用C文件IO的原因仅仅是因为我最初写这些函数是因为我对C++非常陌生,而且我还不太理解所有的IoSoW内容。我仍然在使用这些函数,只是因为一些使用它们的旧代码(无论如何,我打算很快替换它们)。谢谢你的回答。我使用C文件IO的原因仅仅是因为我最初写这些函数是因为我对C++非常陌生,而且我还不太理解所有的IoSoW内容。我仍然在使用这些函数,只是因为一些使用它们的旧代码(无论如何,我打算很快替换它们)。