write and read string to binary file C++

后端 未结 7 752
暖寄归人
暖寄归人 2020-12-09 12:46

Im having problems writing string into a binary file. This is my code:

ofstream outfile(\"myfile.txt\", ofstream::binary);
std::string text = \"Text\";
outfi         


        
相关标签:
7条回答
  • 2020-12-09 13:08

    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.

    0 讨论(0)
  • I had the same problem. I found the perfect answer here: 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.

    0 讨论(0)
  • 2020-12-09 13:15

    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

    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

    0 讨论(0)
  • 2020-12-09 13:16
    • Why are you using pointers to std::string class?
    • 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.

    You should try:

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

    or

    outfile << text;
    
    0 讨论(0)
  • 2020-12-09 13:27

    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.

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

    you can simply write

    outfile << text;
    

    instead.

    0 讨论(0)
  • 2020-12-09 13:31

    To write a std::string to a binary file, you need to save the string length first:

    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 讨论(0)
提交回复
热议问题