[英]C++ fread() into a std::string
Like always, problems with pointers. 像往常一样,指针的问题。 This time I am trying to read a file (opened in binary mode) and store some portion of it in a std::string object. 这次我试图读取一个文件(以二进制模式打开)并将其中的一部分存储在std :: string对象中。 Let's see: 让我们来看看:
FILE* myfile = fopen("myfile.bin", "rb");
if (myfile != NULL) {
short stringlength = 6;
string mystring;
fseek(myfile , 0, SEEK_SET);
fread((char*)mystring.c_str(), sizeof(char), (size_t)stringlength, myfile);
cout << mystring;
fclose(myfile );
}
Is this possible? 这可能吗? I don't get any message. 我没有得到任何消息。 I am sure the file is OK When I try with char* it does work but I want to store it directly into the string. 我确定文件没问题当我尝试使用char *它确实有效但我想将它直接存储到字符串中。 Thanks for your help! 谢谢你的帮助!
Set the string to be large enough first to avoid buffer overrun, and access the byte array as &mystring[0]
to satisfy const
and other requirements of std::string
. 首先将字符串设置得足够大,以避免缓冲区溢出,并将字节数组作为&mystring[0]
以满足std::string
const
和其他要求。
FILE* myfile = fopen("myfile.bin", "rb");
if (myfile != NULL) {
short stringlength = 6;
string mystring( stringlength, '\0' );
fseek(myfile , 0, SEEK_SET);
fread(&mystring[0], sizeof(char), (size_t)stringlength, myfile);
cout << mystring;
fclose(myfile );
}
There are many, many issues in this code but that is a minimal adjustment to properly use std::string
. 此代码中存在许多问题,但这是对正确使用std::string
的最小调整。
string::c_str()
returns const char*
which you can not modify. string::c_str()
返回const char*
,您无法修改它。
One way to do this would be use a char* first and construct a string from it. 一种方法是首先使用char *并从中构造一个字符串。
Example 例
char buffer = malloc(stringlength * sizeof(char));
fread(buffer, sizeof(char), (size_t)stringlength, myfile);
string mystring(buffer);
free(buffer);
But then again, if you want a string, you should perhaps ask yourself Why am I using fopen and fread in the first place??
但话说回来,如果你想要一个字符串,你或许应该问问自己Why am I using fopen and fread in the first place??
fstream
would be a much much better option. fstream
将是一个更好的选择。 You can read more about it here 你可以在这里阅读更多相关信息
I would recommend this as the best way to do such a thing. 我会建议这是做这种事情的最佳方式。 Also you should check to make sure that all the bytes were read. 您还应该检查以确保读取所有字节。
FILE* sFile = fopen(this->file.c_str(), "r");
// if unable to open file
if (sFile == nullptr)
{
return false;
}
// seek to end of file
fseek(sFile, 0, SEEK_END);
// get current file position which is end from seek
size_t size = ftell(sFile);
std::string ss;
// allocate string space and set length
ss.resize(size);
// go back to beginning of file for read
rewind(sFile);
// read 1*size bytes from sfile into ss
fread(&ss[0], 1, size, sFile);
// close the file
fclose(sFile);
Please check out the following regarding c_str to see some things that are wrong with your program. 请查看以下有关c_str的信息,以查看程序中出现的一些问题。 A few issues include the c_str not being modifiable, but also that it returns a pointer to your string contents, but you never initialized the string. 一些问题包括c_str不可修改,但它还返回一个指向字符串内容的指针,但是你从未初始化字符串。
http://www.cplusplus.com/reference/string/string/c_str/ http://www.cplusplus.com/reference/string/string/c_str/
As for resolving it... you could try reading into a char* and then initializing your string from that. 至于解决它...你可以尝试读入char *然后从中初始化你的字符串。
No it is not. 不它不是。 std::string::c_str()
method does not return a modifiable character sequence as you can validate from here . std::string::c_str()
方法不会返回可修改的字符序列,因为您可以从此处进行验证。 A better solution would be using a buffer char
array. 更好的解决方案是使用缓冲区char
数组。 Here is an example: 这是一个例子:
FILE* myfile = fopen("myfile.bin", "rb");
if (myfile != NULL) {
char buffer[7]; //Or you can use malloc() / new instead.
short stringlength = 6;
fseek(myfile , 0, SEEK_SET);
fread(buffer, sizeof(char), (size_t)stringlength, myfile);
string mystring(buffer);
cout << mystring;
fclose(myfile );
//use free() or delete if buffer is allocated dynamically
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.