Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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++ 将字符串写入二进制文件C++;_C++_String_File_Malloc - Fatal编程技术网

C++ 将字符串写入二进制文件C++;

C++ 将字符串写入二进制文件C++;,c++,string,file,malloc,C++,String,File,Malloc,我在将字符串写入二进制文件时遇到问题。这是我的代码: 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("myfi

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

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();
char*buffer=(char*)malloc(sizeof(string));
ifstream-infle(“myfile.txt”,ifstream::binary);
infle.read(缓冲区,sizeof(prueba));
std::string*elem=(string*)缓冲区;

cout可能也应该使用
c_str()
来获取char指针,而不是直接的疯狂转换

outfile.write((char*) &text, sizeof (string));
这是不对的

sizeof(string)
不返回字符串的长度,它返回字符串类型的大小(以字节为单位)

也不要使用C转换将文本转换为
char*
,您可以通过使用适当的成员函数
text.C_str()

你可以简单地写

outfile << text;
outfile
  • 为什么要使用指向
    std::string
    类的指针
  • 您不应该将
    sizeof
    std::string
    一起使用,因为它返回
    std::string
    对象的大小,而不是内部字符串的实际大小
你应该试试:

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

outfile试试这个代码片段

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

是一个很好的例子。

要将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);
因为字符串的大小是可变的,除非将该大小放入文件中,否则将无法正确检索它。您可以依赖“\0”标记,该标记保证位于c字符串或等效字符串::c_str()调用的末尾,但这不是一个好主意,因为 1.您必须逐个字符地读入字符串并检查空值
2.std::string可以合法地包含空字节(尽管它确实不应该包含空字节,因为对c_str()的调用会造成混乱)。

您的代码在写入和读取文件时使用的方式是错误的 和文件扩展名错误您试图读取文本文件
.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();
读取文件

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();
char*buffer=(char*)malloc(sizeof(string));
ifstream-infle(“myfile.dat”,ifstream::binary | ios::in);
infle.read(缓冲区,sizeof(prueba));
std::string*elem=(string*)缓冲区;

我也有同样的问题。我在这里找到了完美的答案:


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

看起来您正在写入字符串对象数据,但这可能不会写入存储在对象中的文本。您应该使用C++中的流。类似于outfile@Mark的快速搜索显示:请发布一些真实的、复制粘贴的代码。假设
string
std::string
,则不会编译:
string*text=“text”这里有很多错误,基本理解C++是如何工作的。你看起来像是一个C程序员,试图让C++的东西工作。你应该停下来读一本关于C++的书。最重要的是,认识到C++是一种不同的动物(一个你几乎不应该使用<代码> MaloC < /COD> >)。@ BelyJava-“快速搜索”在这里没有帮助,因为这个<代码>字符串< /代码>可以是任何东西,不一定是代码> STD::String 。假设它是代码> STD::String 。
字符串以一种非常奇怪的方式被使用。我们感谢你的回答,但这是一个5年前的问题。@Sandeesh我认为这不重要,只要答案是“okay ish”格式,从我的观点来看,这对我来说是有效的,但有一个变化。在编写代码时,我更改了outfile.write(&str[0],size);输出文件。写入(str.c_str(),大小);