简体   繁体   English

将字符串写入和读取到二进制文件 C++

[英]write and read string to binary file C++

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!谢谢!

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)不返回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()也不要使用 C 类型转换将文本转换为char* ,您可以使用适当的成员函数text.c_str()获取 char*

you can simply write你可以简单地写

outfile << text;

instead.反而。

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您可以依赖 '\\0' 标记,该标记保证位于 c 字符串的末尾或等效的 string::c_str() 调用,但这不是一个好主意,因为

  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). std::string 可以合法地包含一个空字节(尽管它确实不应该,因为对 c_str() 的调用会令人困惑)。
  • Why are you using pointers to std::string class?为什么要使用指向std::string类的指针?
  • You should not use sizeof with std::string , as it returns the size of the std::string object, and not the real size of the string inside.您不应将sizeofstd::string ,因为它返回std::string对象的大小,而不是内部字符串的实际大小。

You should try:你应该试试:

string text = "Text";
outfile.write(text.c_str(), text.size());

or或者

outfile << text;

也应该使用c_str()来获取字符指针,而不是直接的疯狂转换。

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试试这个它会工作

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.关键问题:在写出时使用 string::length 获取字符串的长度,并在读取字符串之前使用 resize()。 And both for reading and writing, use mystring.c_str() instead the string itself.对于读取和写入,请使用 mystring.c_str() 而不是字符串本身。

Try this code snippet.试试这个代码片段。

/* writing string into a binary file */

  fstream ifs;
  ifs.open ("c:/filename.exe", fstream::binary | fstream::in | fstream::out);

  if (ifs.is_open())
  {
   ifs.write("string to binary", strlen("string to binary")); 
   ifs.close();
  }

Here is a good example.是一个很好的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM