Writing char* to binary file using ostream::write

后端 未结 1 1792
时光取名叫无心
时光取名叫无心 2021-01-03 16:18

I am trying to write a char* to a binary file.

This is what I have now.

void Write(char* fileName, char* pData)
{

    ofstream binFile (fileName, io         


        
1条回答
  •  轮回少年
    2021-01-03 16:52

    binFile.write((char*)&pData,         sizeof(pData));
    

    is wrong. It just writes the value of the pointer. It does not write the data.

    You need to use:

    binFile.write(pData, strlen(pData));
    

    However, that won't be adequate to read the data back. To be able to read the data back, you'll need to write the size of the string first.

    size_t len = strlen(pData);
    binFile.write((char*)&len, sizeof(len));
    binFile.write(pData, len);
    

    And when reading the data back, you will need to use:

    size_t len = 0;
    binFile.read(char*)&len, sizeof(len));
    binFile.read(pData, len);
    

    and then, null terminate the string.

    pData[len] = '\0';
    

    PS

    Make sure getTestData is properly initialized before using it to read the data.

    char getTestData[100];
    

    will be adequate for your test case.

    Update

    You can make your program a bit better by using std::string instead of char*. The size of the saved data can be more easily managed when a std::string is used.

    void Write(std::string const& fileName, std::string const& data)
    {
       std::ofstream binFile(fileName, std::ios::out | std::ios::binary);
       if (binFile.is_open())
       {
          size_t len = data.size();
          binFile.write((char*)&len, sizeof(len));
          binFile.write((char*)&data[0], len);
    
          // No need. The file will be closed when the function returns.
          // binFile.close();
       }
    }
    
    void Read(std::string const& fileName, std::string& data)
    {
       std::ifstream binFile(fileName, std::ios::in | std::ios::binary);
       if(binFile.is_open())
       {
          size_t len = 0;
          binFile.read((char*)&len, sizeof(len));
          data.resize(len);
          binFile.read((char*)&data[0], len);
       }
    }
    
    int main()
    {
       std::string file = "testData.dat";
    
       std::string testData = "ABCdEFG";
       Write(file, testData);
    
       std::string getTestData;
       Read(file, getTestData);
    
       std::cout << getTestData << std::endl;
    }
    

    0 讨论(0)
提交回复
热议问题