【问题标题】:write and read string to binary file C++将字符串写入和读取到二进制文件 C++
【发布时间】:2012-06-08 01:24:16
【问题描述】:

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

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

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

【问题讨论】:

  • 看起来您正在编写字符串对象数据,但这可能不会写入存储在对象中的 TEXT。您应该在 C++ 中使用 Streams。相当于 outfile
  • @Mark 快速搜索显示:cplusplus.com/reference/string/string
  • 请发布一些真实的、复制粘贴的代码。假设 stringstd::string,这不会编译:string *text = "Text";
  • 这里有很多错误,基本理解 C++ 是如何工作的。你看起来像是一个试图让 C++ 工作的 C 程序员。你应该停下来阅读一本关于 C++ 的书。最重要的是,要意识到 C++ 是一种不同的动物(你几乎不应该使用malloc)。
  • @BillJames - “快速搜索”在这里没有帮助,因为 string 可以是任何东西,不一定是 std::string

标签: c++ string file malloc


【解决方案1】:

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

【讨论】:

  • 假设它 std::stringstring 的使用方式很奇怪。
【解决方案2】:

线

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;
    

    【讨论】:

      【解决方案4】:

      试试这个代码 sn-p。

      /* 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 就是一个很好的例子。

      【讨论】:

        【解决方案5】:

        要将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-string 或等效 string::c_str() 调用末尾的 '\0' 标记,但这不是一个好主意,因为

        1. 您必须逐个字符地读取字符串中的空值
        2. 一个 std::string 可以合法地包含一个空字节(尽管它确实不应该因为调用 c_str() 会令人困惑)。

        【讨论】:

        • 这对我有用,只需进行一项更改。在编写代码中,我更改了 outfile.write(&str[0],size);到 outfile.write(str.c_str(),size);
        【解决方案6】:

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

        试试这个吧

        【讨论】:

        • 感谢您的回答,但这是一个 5 年前的问题。
        • @Sandeesh 我认为这无关紧要,只要答案是“okay-ish”格式,从我的角度来看就是这样
        【解决方案7】:

        我遇到了同样的问题。我在这里找到了完美的答案:Write file in binary format

        关键问题:写出时使用string::length获取字符串长度,读取字符串前使用resize()。对于阅读和写作,使用 mystring.c_str() 代替字符串本身。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-18
          相关资源
          最近更新 更多