将字符串读写到二进制文件中的C++代码

13

我在写入字符串到二进制文件时遇到了问题。这是我的代码:

ofstream outfile("myfile.txt", ofstream::binary);
std::string text = "Text";
outfile.write((char*) &text, sizeof (string));
outfile.close();

然后,我试着去阅读它,

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();

我就是无法让它工作。对不起,我只是绝望了。谢谢!


看起来你正在编写字符串对象数据,但这可能不会写入对象中存储的文本。在C++中,你应该使用流。等同于outfile << text。 - billjamesdev
1
请贴上一些真实的、复制粘贴的代码。假设 stringstd::string,这段代码是无法编译通过的:string *text = "Text"; - jrok
2
这里有很多问题,基本上是对C++工作原理的误解。你似乎是一位试图让C++工作的C程序员。你应该停下来读一本关于C++的书。最重要的是,认识到C++是一个不同的动物(几乎永远不应该使用“malloc”)。 - Nicol Bolas
2
@BillJames - “快速搜索”在这里没有帮助,因为这个“string”可以是任何东西,不一定是“std::string”。 - Kiril Kirov
可能是c++ - properly writing std::string to binary file的重复问题。 - Hcorg
显示剩余3条评论
7个回答

13

要将std::string写入二进制文件,需要先保存字符串的长度:

std::string str("whatever");
size_t size=str.size();
outfile.write(&size,sizeof(size));
outfile.write(&str[0],size);

要将其读入,请反转该过程,首先调整字符串大小,以便您有足够的空间:

std::string str;
size_t size;
infile.read(&size, sizeof(size));
str.resize(size);
infile.read(&str[0], size);

因为字符串的大小是可变的,除非在文件中放置该大小,否则您将无法正确地检索它。您可以依赖于位于c字符串末尾且保证存在的'\ 0'标记或等效的string :: c_str()调用,但这不是一个好主意,因为

  1. 您必须逐个字符读入字符串并检查空值。
  2. std :: string可以合法地包含null字节(虽然实际上不应该包含,因为对c_str()的调用会很困惑)。

4
我曾用这种方法成功了,只做了一个更改。在编写代码时,我将outfile.write(&str[0],size); 改为 outfile.write(str.c_str(),size);。 - Scott Hutchinson

12

这条线

outfile.write((char*) &text, sizeof (string));

不正确。

sizeof(string)并不返回字符串的长度,而是返回字符串类型在字节中的大小。

同时,不要使用C风格的强制类型转换将文本转换为char*,可以使用相应的成员函数text.c_str()获取char*

你可以直接编写:

outfile << text;

替代方法。


3
  • 为什么要使用指向std::string类的指针?
  • 不应该使用sizeofstd::string一起使用,因为它返回的是std::string对象的大小,而不是字符串内部的实际大小。

您应该尝试:

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

或者

outfile << text;

1

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


假设它确实是“std :: string”。这个“string”被以一种相当奇怪的方式使用。 - chris

0

我曾经遇到过同样的问题。我在这里找到了完美的答案:以二进制格式写入文件

关键问题:在写出字符串时使用string::length获取字符串长度,在读取字符串之前使用resize()。而且在读取和写入时,都要使用mystring.c_str()而不是字符串本身。


0

你的代码有问题,你使用了错误的方式来写入和读取文件,并且你正试图读取文本文件的文件扩展名为.txt的错误。正确的代码是:

写入文件

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();

读取文件

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();

试一下,它会起作用的


我们感谢您的回答,但这是一个五年前的问题。 - Sandeesh
1
@Sandeesh 我认为这并不重要,只要答案的格式“还行”,从我的角度来看是可以的。 - Eyk Rehbein

-1

试试这段代码片段。

/* 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();
  }

这里是一个很好的例子。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接