使用ostream :: write将char *写入二进制文件 [英] Writing char* to binary file using ostream::write
问题描述
我正在尝试将char *写入二进制文件.
I am trying to write a char* to a binary file.
这就是我现在拥有的.
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?
推荐答案
binFile.write((char*)&pData, sizeof(pData));
是错误的.它只是写指针的值.它不会写入数据.
is wrong. It just writes the value of the pointer. It does not write the data.
您需要使用:
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);
,然后,将字符串终止为null.
and then, null terminate the string.
pData[len] = '\0';
PS
在使用getTestData
读取数据之前,请确保已正确初始化getTestData
.
Make sure getTestData
is properly initialized before using it to read the data.
char getTestData[100];
对于您的测试用例就足够了.
will be adequate for your test case.
更新
通过使用std::string
而不是char*
,可以使您的程序更好.使用std::string
时,可以更轻松地管理保存的数据的大小.
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;
}
这篇关于使用ostream :: write将char *写入二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!