Writing char* to binary file using ostream::write

2020-03-08 05:26发布

问题:

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, ios::out | ios::binary);
    if (binFile.open())
    {
        binFile.write((char*)&pData,         sizeof(pData));
        binFile.close();
    }
}


void Read(char* fileName, char* pData)
{
    ifstream binFile(fileName, ios::in | ios::binary);
    if(binFile.open())
    {
        binFile.read(char*)&pData, sizeof(pData));
        binFile.close
    }
}

int main()
{
    char* testData = "ABCdEFG"; // not real data
    char* getTestData;
    char* file = "C:\\testData.dat";
    Write(file, testData);
    Read(file, getTestData);
}

Test data will be of unknown length. May not always be the same.

When i run the program once, and write and read. I can get back the test data.

But when i stop the program and run it again, this time without writing. Just reading, i cannot get back the test data.

I don't really understand whats happening here. Can some one explain it to me?

回答1:

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;
}