将字符串写入和读取到二进制文件 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10873382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
write and read string to binary file C++
提问by Keng92pd
Im having problems writing string into a binary file. This is my code:
我在将字符串写入二进制文件时遇到问题。这是我的代码:
ofstream outfile("myfile.txt", ofstream::binary);
std::string text = "Text";
outfile.write((char*) &text, sizeof (string));
outfile.close();
Then, I try to read it,
然后,我尝试阅读它,
char* buffer = (char*) malloc(sizeof(string));
ifstream infile("myfile.txt", ifstream::binary);
infile.read(buffer, sizeof (prueba));
std::string* elem = (string*) buffer;
cout << *elem;
infile.close();
I just cant get it to work. I am sorry, I am just desperate. Thank you!
我只是无法让它工作。对不起,我只是绝望了。谢谢!
回答by AndersK
the line
线
outfile.write((char*) &text, sizeof (string));
is not correct
是不正确的
sizeof(string)
doesn't return the length of the string, it returns the sizeof the string type in bytes.
sizeof(string)
不返回字符串的长度,它以字节为单位返回字符串类型的大小。
also do not cast text to char*
using a C cast, you can get hold of the char* by using the appropriate member function text.c_str()
也不要将文本转换为char*
使用 C 转换,您可以通过使用适当的成员函数来获取 char*text.c_str()
you can simply write
你可以简单地写
outfile << text;
instead.
反而。
回答by Pablo Alvarez
To write a std::string to a binary file, you need to save the string length first:
要将 std::string 写入二进制文件,您需要先保存字符串长度:
std::string str("whatever");
size_t size=str.size();
outfile.write(&size,sizeof(size);
outfile.write(&str[0],size);
To read it in, reverse the process, resizing the string first so you will have enough space:
要读取它,请反转该过程,首先调整字符串的大小,以便您有足够的空间:
std::string str;
size_t size;
infile.read(&size, sizeof(size));
str.resize(size);
infile.read(&str[0], size);
Because strings have a variable size, unless you put that size in the file you will not be able to retrieve it correctly. You could rely on the '\0' marker that is guaranteed to be at the end of a c-string or the equivalent string::c_str() call, but that is not a good idea because 1. you have to read in the string character by character checking for the null 2. a std::string can legitimately contain a null byte (although it really shouldn't because calls to c_str() are then confusing).
因为字符串的大小是可变的,除非您将该大小放入文件中,否则您将无法正确检索它。您可以依赖 '\0' 标记,该标记保证位于 c 字符串的末尾或等效的 string::c_str() 调用,但这不是一个好主意,因为 1. 您必须阅读字符串逐字符检查空值 2. std::string 可以合法地包含空字节(尽管它确实不应该,因为对 c_str() 的调用会造成混淆)。
回答by Baget
- Why are you using pointers to
std::string
class? - You should not use
sizeof
withstd::string
, as it returns the size of thestd::string
object, and not the real size of the string inside.
- 为什么要使用指向
std::string
类的指针? - 您不应该使用
sizeof
withstd::string
,因为它返回std::string
对象的大小,而不是内部字符串的实际大小。
You should try:
你应该试试:
string text = "Text";
outfile.write(text.c_str(), text.size());
or
或者
outfile << text;
回答by PTT
回答by Bryan
Should probably also use c_str()
to get the char pointer too, instead of that straight crazy cast.
也应该c_str()
用来获取字符指针,而不是直接的疯狂转换。
回答by Shahzaib Chadhar
Your code is wrong wrong way you are using to write & read the file
and file extension error you are trying to read text file .txt
correct code
您的代码是错误的,您使用错误的方式来编写和读取文件和文件扩展名错误,您正在尝试读取文本文件.txt
正确的代码
Write to file
写入文件
std::string text = "Text";
ofstream outfile("myfile.dat", ofstream::binary | ios::out);
outfile.write(&text,sizeof (string));//can take type
outfile.write(&text,sizeof (text));//can take variable name
outfile.close();
reading file
读取文件
char* buffer = (char*) malloc(sizeof(string));
ifstream infile("myfile.dat", ifstream::binary | ios::in);
infile.read(buffer, sizeof (prueba));
std::string* elem = (string*) buffer;
cout << *elem;
infile.close();
Try This it will work
试试这个它会工作
回答by Machiel Kolstein
I had the same problem. I found the perfect answer here: Write file in binary format
我有同样的问题。我在这里找到了完美的答案:Write file in binary format
Key issues: use string::length to get the length of the string when writing out and use resize() before reading the string. And both for reading and writing, use mystring.c_str() instead the string itself.
关键问题:在写出时使用 string::length 获取字符串的长度,并在读取字符串之前使用 resize()。对于读取和写入,请使用 mystring.c_str() 而不是字符串本身。